mockingbird icon indicating copy to clipboard operation
mockingbird copied to clipboard

Need help unit testing an iMessage app extension with Mockingbird

Open tedrog36 opened this issue 3 years ago • 2 comments

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.

TestGenerateMocksWSNoPods.zip

tedrog36 avatar Nov 17 '21 23:11 tedrog36

Looking for some feedback/help on this issue.

tedrog36 avatar Dec 31 '21 14:12 tedrog36

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

MyExtensionMyExtensionFrameworkMyExtensionFrameworkTests

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.

andrewchang-bird avatar Jan 03 '22 06:01 andrewchang-bird