【发布时间】:2014-02-28 09:17:11
【问题描述】:
我有一个使用 OCMock 的测试用例,它执行以下操作:
CAAOAuth2AuthenticationManager *oAuth2AuthManager = [[CAAOAuth2AuthenticationManager alloc] init];
id authDelegate = [OCMockObject mockForProtocol:@protocol(CAAAuthenticationDelegate)];
id partialAuthManagerMock = [OCMockObject partialMockForObject:oAuth2AuthManager];
id resultMock = [OCMockObject mockForClass:[CAAOAuth2AuthenticationResult class]];
[[authDelegate reject] didFailWithError:OCMOCK_ANY];
[[[partialAuthManagerMock expect] andForwardToRealObject] authenticateWithResult:OCMOCK_ANY formData:OCMOCK_ANY delegate:authDelegate];
[[partialAuthManagerMock reject] authenticateWithOptions:OCMOCK_ANY delegate:authDelegate];
[[[resultMock expect] andReturnValue:OCMOCK_VALUE(YES) ] isAuthenticated];
[[resultMock reject] refreshToken];
当我运行测试用例时,同样使用 CAAAuthenticationDelegate 协议的第二个测试用例(完全不同的测试类和文件)因 SIGABRT 失败:
2014-02-28 10:11:24.594 otest[37161:303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'OCMockObject[CAAAuthenticationDelegate]: unexpected method invoked: didReceiveAuthenticationWithResult:OCMockObject[CAAOAuth2AuthenticationResult]
stubbed: didFailWithError:<OCMAnyConstraint: 0xa913fc0>'
但是,我在第二个测试用例中没有使用任何模拟。我试图用stopMocking 清除模拟但没有成功。
以下模拟设置没有任何问题:
[[authDelegate reject] didFailWithError:OCMOCK_ANY];
[[[partialAuthManagerMock expect] andForwardToRealObject] authenticateWithResult:OCMOCK_ANY formData:OCMOCK_ANY delegate:authDelegate];
[[partialAuthManagerMock expect] authenticateWithOptions:OCMOCK_ANY delegate:authDelegate];
[[[resultMock expect] andReturnValue:OCMOCK_VALUE(NO) ] isAuthenticated];
[[[resultMock expect] andReturn:refreshToken] refreshToken];
谁能告诉我,为什么会这样?
【问题讨论】:
-
抛出异常时完整的堆栈跟踪是什么?你在某个地方留下了一个在记忆中晃来晃去的模拟。很可能是你的 andForwardToRealObject,这是在做任何类型的异步活动,测试完成后可能会在内存中徘徊吗?
标签: ios unit-testing ocmock sentestingkit