mockingbird
mockingbird copied to clipboard
Need help unit testing an iMessage app extension with Mockingbird
Description
I want to use mocking in my unit test target for an iMessage app extension. Everything I have tried so far as not worked, meaning I cannot get it to link without errors. I am guessing is it tied to the fact that the test is not using a host application.
I have attached a sample application that illustrates the issue I am having. The target MessageExtensionTests fails to link when I try to test MessageExtension. To make the project smaller I removed the Pods directory so you would need to run 'pod install'.
Environment
- Mockingbird CLI version: 0.18.1
- Xcode and macOS version: Xcode 13.1, Mac OS: 11.6
- Swift version: 5.5.1
- Installation method: CocoaPods
- Unit testing framework: XCTest
- Does your project use
.mockingbird-ignore
? No - Are you using [supporting source files] I think so.
Looking for some feedback/help on this issue.
I am guessing is it tied to the fact that the test is not using a host application.
Right, extensions themselves require a host app to run so you can’t use one as a host app for the test. Note that this isn’t a limitation with Mockingbird, but with the iOS plugin architecture.
To unit test extensions, typically you’d factor out the code you want to test into a cross-platform framework and test the framework. So your dependency graph would look something like
MyExtension
→ MyExtensionFramework
← MyExtensionFrameworkTests
It is possible to do some dark magic by using the original iOS app as the host and loading the extension symbols at runtime with dlsym
, but it’s not going to be pretty or maintainable.