2011-01-14 17 views
1

コントローラークラスのIBoutletがすべてNIBファイルに正しく接続されていることをユニットテストで検証したいと考えています。私はOCMockでこれをやりたいのですが、コントローラの変数がNIBをロードした後にnilではないことがわかりました。これは、プロセスがどのように動作するかを一般的に理解する上で、私が理解する限り、これは動作するはずです。OCMockとのIBOutlet接続の確認

NIB OnOffSwitchCellはファイル所有者がOnOffSwitchCellControllerです。 これは私の試験方法である:

- (void) testIBOutletCellIsWiredToXib { 
    id mockController = [OCMockObject mockForClass:[OnOffSwitchCellController class]]; 
    [[mockController expect] awakeAfterUsingCoder:OCMOCK_ANY]; 
    [[mockController expect] setValue:OCMOCK_ANY forKey:@"cell"]; 
    [[mockController expect] setValue:OCMOCK_ANY forKey:@"thelabel"]; 
    [[mockController expect] setValue:OCMOCK_ANY forKey:@"theswitch"]; 

    NSArray* nibContents = [guiBundle loadNibNamed:@"OnOffSwitchCell" 
              owner:mockController 
              options:nil]; 
    assertThat(nibContents, isNot(nil)); 
    assertThatInt([nibContents count], is(equalToInt(1))); 
    assertThat([nibContents objectAtIndex:0], is(instanceOf([OnOffSwitchCell class]))); 

    [mockController verify]; 
} 

guiBundleが存在し、有効NSBundleオブジェクトであることが確認されました。

loadNibNamed:owner:options:はNIB内のオブジェクトを逆シリアル化しますので、awakeAfterUsingCoder:に電話をかけてから、それぞれにsetValue:forKey:を呼び出してコンセントを設定してください。

ロードされたNIBに実際に正しいオブジェクトが含まれていることを確認するために3つのアサートを追加しました。これらは実際のコントローラのインスタンスに入れても問題ありません。しかし、私が上記のようにモックを使用すると、これまでのところさえ得られません。代わりに、これでテストクラッシュ:

 
Test Case '-[OnOffSwitchCellControllerTestCase testIBOutletCellIsWiredToXib]' started. 
2011-01-14 10:48:35.364 GTMTest[67797:903] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
    reason: 'OCMockObject[OnOffSwitchCellController]: 
       unexpected method invoked: awakeAfterUsingCoder:<UINibDecoder: 0x500e800> 
    expected: setValue:<OCMAnyConstraint: 0x4c718e0> forKey:@"cell" 
    expected: setValue:<OCMAnyConstraint: 0x4c71ce0> forKey:@"thelabel" 
    expected: setValue:<OCMAnyConstraint: 0x4c71ed0> forKey:@"theswitch"' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00e3dbe9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x00f925c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00e3db21 -[NSException raise] + 17 
    3 GTMTest        0x0001a049 -[OCMockObject handleUnRecordedInvocation:] + 322 
    4 GTMTest        0x00019aca -[OCMockObject forwardInvocation:] + 77 
    5 CoreFoundation      0x00daf404 ___forwarding___ + 1124 
    6 CoreFoundation      0x00daef22 _CF_forwarding_prep_0 + 50 
    7 UIKit        0x0062394a UINibDecoderDecodeObjectForValue + 2438 
    8 UIKit        0x00624693 -[UINibDecoder decodeObjectForKey:] + 398 
    9 UIKit        0x0053cf43 -[UIRuntimeConnection initWithCoder:] + 212 
    10 UIKit        0x0053d4b1 -[UIRuntimeEventConnection initWithCoder:] + 64 
    11 UIKit        0x006239e4 UINibDecoderDecodeObjectForValue + 2592 
    12 UIKit        0x006232dc UINibDecoderDecodeObjectForValue + 792 
    13 UIKit        0x00624693 -[UINibDecoder decodeObjectForKey:] + 398 
    14 UIKit        0x0053c200 -[UINib instantiateWithOwner:options:] + 804 
    15 UIKit        0x0053e081 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168 
    16 GTMTest        0x000140dc -[OnOffSwitchCellControllerTestCase testIBOutletCellIsWiredToXib] + 503 
    17 GTMTest        0x000041f3 -[SenTestCase invokeTest] + 163 
    18 GTMTest        0x0000479a -[GTMTestCase invokeTest] + 146 
    19 GTMTest        0x00003e90 -[SenTestCase performTest] + 37 
    20 GTMTest        0x00002f3d -[GTMIPhoneUnitTestDelegate runTests] + 1413 
    21 GTMTest        0x000028fb -[GTMIPhoneUnitTestDelegate applicationDidFinishLaunching:] + 197 
    22 UIKit        0x00347253 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1252 
    23 UIKit        0x0034955e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439 
    24 UIKit        0x00348ef0 -[UIApplication _run] + 452 
    25 UIKit        0x0035542e UIApplicationMain + 1160 
    26 GTMTest        0x00003500 main + 104 
    27 GTMTest        0x0000273d start + 53 
    28 ???         0x00000002 0x0 + 2 
) 
terminate called after throwing an instance of 'NSException' 

は、だから、私は明らかにそれを期待していても、予期しないものとしてawakeAfterUsingCoder:への呼び出しを不平を言っています。

私はまた、その期待を取り除き、余分なメソッド呼び出しを報告しない素敵なモックでモックを置き換えようとしましたが、それでも呼び出されずにsetValue:forKey:が呼び出されないと報告します。

私はここで何が欠けていますか?

答えて

0

これをメインのシステムスレッドで実行しようとしましたか?メインスレッドからUIKitクラスをインスタンス化することはできません。 GTMはそれをしないか、しかし、あなたのテストケースクラスに次のように置くことができるGHUnitとわからない:

- (BOOL)shouldRunOnMainThread { 
    /* Tell GHUnit to run on a separate thread */ 
    return YES; 
} 
+0

残念なことに、違いはありません。 –

-1

あなたが実際にユニットテスト内の任意の視覚的なものを行う傾けます。ビューは実際にはロードされません。また、awakeAfterUsingCoderが呼び出されたことをテストする必要はありません。それはAppleのものだ。通常、ユニットテストはロジックをテストするだけです。 Appleのコードなので、awakeAfterUsingCoderがinitから呼び出されると、安全に仮定できます。あなたのメソッドが呼び出されることを確認するだけです

関連する問題