I need to integrating unit test for my app with Realm Swift. But it always report issue even import RealmSwift
has been added:
Missing required modules: 'Realm.Private', 'Realm'
Then I tried to add Realm
& realmSwift
in "Build Phase/Link Binary With Libraries", but it crashes directly with error report:
Class _TtGC10RealmSwift4ListC3AMA14ROOperatorItem_ is implemented in both ?? (0x7fdf040223b8) and ?? (0x7fdf040c8468). One of the two will be used. Which one is undefined.
Finally I found the solution on its official website https://www.mongodb.com/docs/realm/sdk/swift/test-and-debug/
But I’m using SPM to manage packages, not the local embed Realm
& RealmSwift
frameworks, in such a way, what’s the parent path to RealmSwift.framework?
You can download a simple sample project that reproduce the issue from OneDrive:
RealmUnitTestError_GitHub
This is a similar question on stackoverflow, but all answers are not work now:
"Missing required module 'XYZ'" on Unit Tests when using Swift Package Manager
And some discussions on Github:
https://github.com/apple/swift-nio/issues/1128#issuecomment-527566361
Thank you very much in advance!
3
Answers
Try this: add /path/to/RealmSwift.framework/Frameworks to the “Framework Search Paths” section in Build Settings.
In more details: Adding RealmSwift as a subproject: Missing required modules: 'Realm.Private', 'Realm'
There appears to be a ‘fix’ for this so posting it as an answer for completeness.
The issue is a conflict and/or disconnect between the Realm Library and RealmSwift Library. The fix is to change the Linked Binaries in the Test targets
On the main app target, leave the Realm and RealmSwift libraries in the Linked Binary With Library section
On the Test Target(s) remove the RealmSwift library from the Linked Binary With Library section and leave only the Realm library
It works for me: select Build Phases – Link Binary With Libraries in xxxTest.xcodeproj file, and add RealmSwift library.