Search code examples
iosswiftunit-testingrealm

Realm Swift Unit Test Error: Missing required modules: 'Realm.Private', 'Realm'?


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/

enter image description here

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!


Solution

  • 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

    1. On the main app target, leave the Realm and RealmSwift libraries in the Linked Binary With Library section enter image description here

    2. On the Test Target(s) remove the RealmSwift library from the Linked Binary With Library section and leave only the Realm library enter image description here