iOS Unit Test Fails - Unable To Access KeyChain

3.9k Views Asked by At

I'm trying to write unit tests for an iOS framework I'm creating. Under the covers it uses Nuance's Dragon Mobile SDK. When I run my unit test in Jetbrains' AppCode, I get this error:

2012-08-15 17:43:48.874 otest[62547:707] *** Assertion failure in -[KeychainManager writeToKeychain], /checkout_120606-1556/NMSPClient/sdk/c/trunk/solutions/iPhone/NMSP/../../../source/oem/iphone/src/KeychainManager.m:285

Couldn't add the Keychain Item.
Unknown.m:0: error: -[NuanceVoiceRecognizerTest testExample] : Couldn't add the Keychain Item.

When I run the test in XCode, it works fine. In fact XCode spins up the simulator to run the test, whereas AppCode doesn't seem to want/need to.

Now, the reason the Nuance bit is relevant is that it's the thing that's trying to do stuff with the keychain when I set it up:

[SpeechKit setupWithID:@"<snip>"
    host:@"sandbox.nmdp.nuancemobility.net"
    port:443
    useSSL:NO 
    delegate:self];

Now I know one answer here is to just run the test in XCode. But I have a real hard time with XCode, given how primitive it is on the code-editing front. More importantly, I'm missing some key concept here and that's annoying me.

To the question: What might be missing that would cause my code to fail to access the keystore when the unit test runs? Anyone run into this sort of thing before, even in XCode?

2

There are 2 best solutions below

0
On

If they're not already set, you could try editing the BUNDLE_LOADER and TEST_HOST build settings for your unit test target to point to your app bundle (probably something like $(BUILT_PRODUCTS_DIR)/AppName.app/AppName).

My thinking here is that keychain access presumably (I haven't used it) needs to happen from within a running app. These two settings are what make OCUnit unit tests run within the app process (ie. making them, in Apple's terminology, 'Application Tests' as opposed to 'Logic Tests').

The unit test bundle set up by Xcode in the project wizard is set up as an application test, but subsequently-added test bundles added are logic tests. If you added the unit test target later, it's possible it doesn't have the above build settings.

As to why Xcode runs the tests as-is: from my testing it seems that Xcode always fires up the simulator, and runs tests within the main application, regardless of the bundle loader and test hosts settings. I think this is incorrect behaviour according to Apple's documentation, but it seems to be what it does.

0
On

Here is the AppCode's forum discussion: http://devnet.jetbrains.net/message/5466009#5466009