I'm running swift test
from the command line to run the test cases. This is the test case:
import XCTest @testable import vnk_swift class KeyMappingTests: XCTestCase { static var allTests : [(String, (KeyMappingTests) -> () throws -> Void)] { return [ // ("testExample", testExample), ] } func testExample() { let keyMapping = KeyMapping() XCTAssertNotNil(keyMapping , "PASS") } }
And here is the output message.
If I remove the usage of KeyMapping
, everything works fine:
func testExample() { // let keyMapping = KeyMapping() XCTAssertNotNil(true , "PASS") }
Looks like there is a problem when I'm trying to use a class. How do I fix this?
(I did not use Xcode for this project as I started with swift package init
, the source code for this project is here: https://github.com/trungdq88/vnk-swift)
Why Is the Undefined Symbols for Architecture x86_64: Error Happening? This error is happening due to the lack of included values inside the declared statements in your code. The browser is going to render the information incorrectly and show this error, especially if you are working with Main and Similarity tools.
The error Undefined symbols for architecture arm64: "_OBJC_CLASS_$_SKAdImpression" during the iOS build usually happens if XCode or CocoaPods version is lower than required. To fix it update XCode to 12.5 or higher and CocoaPods to 1.10. 0 or higher.
A symbol remains undefined when a symbol reference in a relocatable object is never matched to a symbol definition. Similarly, if a shared object is used to create a dynamic executable and leaves an unresolved symbol definition, an undefined symbol error results.
I managed to successfully build and test your package by doing the following modifications:
VnkSwift
, for some reasons the build tool doesn't like dashes in package name, nor it works when you have underscores in the generated package name (so renaming the package to vnk_swift
to make sure the import statement and the package name match didn't work)VnkSwiftTests
in order for the linker to know what to link against; seems this is a precondition for the linker to know to link against the packagemain.swift
to something else (I used utils.swift
). The thing is that the presence of main.swift
instructs the build tool to generate an executable, and linking against an executable doesn't work very well. After the rename, had to comment the if
code, as global running code can only belong to main.swift
.To conclude:
main.swift
file to make sure the package can be linked againstWhy can't a unit test target link against executables?
It's because both the test bundle and the executable bundle have global executing code (aka the main function), so the linker doesn't know which one of them to pick. When testing from Xcode, the test bundle runs into the context of the application - it doesn't link against it, like in the situation right here.
Xcode also has this problem when creating a command line tool - you can't unit test that target, if you want to unit test the code then you have to create a library that will be linked by both the tool and the unit test target (or include the files in both targets)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With