我正在为委托(delegate)对象设置一个模拟对象,以检查当 URL 为 nil 时,以 nil 作为参数调用委托(delegate)方法。
当FileDownloadOperation
表现如预期,测试通过,这很好。
当FileDownloadOperation
不调用委托(delegate)方法,测试按预期失败。
但是当FileDownloadOperation
用 nil
以外的东西调用委托(delegate)方法,而不是失败,测试崩溃并且没有其他测试被执行,因为 OCMock
抛出:
'NSInternalInconsistencyException' 原因:'OCMockObject [FileDownloadOperationTest]:调用了意外的方法:数据:<> forURL:nil
-(void) testNilURL{
// 1. Create an operation
FileDownloadOperation * anOp = [[FileDownloadOperation alloc]init];
// 2. set a nil URL
anOp.URL = nil;
// 3. set a mock delegate
id mockDelegate = [OCMockObject mockForClass:[self class]];
[[mockDelegate expect] data:[OCMArg isNil] forURL:[OCMArg isNil]];
anOp.delegate = mockDelegate;
// 4. launch operation
[anOp main];
// 5. ASSERT mock delegate is called with nil data
STAssertNoThrow([mockDelegate verify], @"Delegate should be called with nil data and nil URL");
[anOp release];
}
这是预期的行为吗?还是我做错了什么?
谢谢 !
最佳答案
OCMock 抛出异常来报告不匹配,信任 OCUnit 来捕获和报告任何异常。但是due to a bug in the iOS Simulator ,单元测试无法捕获异常,因此简单地崩溃。
(我目前正在编写一个新的模拟框架,通过不依赖异常来解决这个问题。)
关于ios - 当参数不是预期的参数时,OCMock 抛出 NSInternalInconsistencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5911760/