【发布时间】:2012-04-16 15:27:49
【问题描述】:
问题的简短版本:
以下 Kiwi/iOS 模拟期望有什么问题?
[[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];
长版问题:
我正在尝试在 iOS 的 Kiwi 中为一个处理 NSConnection 的简单类编写测试。为了测试该类是否处理来自 NSConnection 的回调,我将其发送给 NSConnection 通常执行的委托方法。我在班级中有一个代表,可以将数据发送回使用我班级的任何人。要测试我的课程,我必须注入一个模拟委托,然后检查是否调用了我想要的方法。就这么简单:)
我的 Kiwi 测试代码是:
//Some ivars declared elsewhere:
testString1 = @"asd323/4 d14";
testString2 = @"as98 /2y9h3fdd14";
testData1 = [testString1 dataUsingEncoding:NSUTF8StringEncoding];
testData2 = [testString2 dataUsingEncoding:NSUTF8StringEncoding];
mockURLRespons = [NSHTTPURLResponse mock];
int value = 11111;
id mockDelegate = [KWMock mockForProtocol:@protocol(SharepointConnectionDelegate)];
communicator = [[SharepointCommunicator alloc] init];
it (@"should send recieve data back to delegate2", ^{
[communicator setDelegate:mockDelegate];
[mockURLRespons stub:@selector(statusCode) andReturn:theValue(value)];
[(id)communicator connection:niceMockConnector didReceiveResponse:mockURLRespons];
[(id)communicator connection:niceMockConnector didReceiveData:testData1];
[(id)communicator connection:niceMockConnector didReceiveData:testData2];
[(id)communicator connectionDidFinishLoading:niceMockConnector];
[[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];
});
在我的 SharepointCommunicator.m 中:
-(void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response {
if (connection != aConnection) {
[connection cancel];
connection = aConnection;
}
responseData = [[NSMutableData alloc] init];
statusCode = [(NSHTTPURLResponse*)response statusCode];
}
-(void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data {
if (aConnection != self.connection)
return;
[responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *txt = [[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding];
NSLog(@"Statuscode: %i", statusCode);
NSLog(@"Data is: %@",txt);
[delegate connectionDidSucceedWithText:txt andStatus:statusCode];
[self.connection cancel];
self.connection = nil;
}
此代码有效且正确。使用检查点对其进行调试表明它按预期进行。 statusCode 的值为 11111。txt 为 testString1+textString2。它仍然在测试的最后一行失败,并出现以下错误:
error: -[kiwiSharepointCommunicatorTest Sharepointcommunicator_AStateTheComponentIsIn_ShouldSendRecieveDataBackToDelegate2] : 'Sharepointcommunicator, a state the component is in, should send recieve data back to delegate2' [FAILED], mock received unexpected message -connectionDidSucceedWithText:"asd323/4 d14as98 /2y9h3fdd14" andStatus:11111
Test Case '-[kiwiSharepointCommunicatorTest Sharepointcommunicator_AStateTheComponentIsIn_ShouldSendRecieveDataBackToDelegate2]' failed (3.684 seconds).
删除测试中的最后一行仍然会产生相同的错误。我想我对 receive:withArguments: 的理解是错误的..
【问题讨论】:
-
我假设它与 Kiwi 的参数匹配有关。也许匹配是在指向对象的指针上?这就是为什么字符串不匹配的原因,即使它们包含相同的字符串?或者它可能无法处理诸如 ints 之类的非对象的匹配...
-
我遇到了类似的问题,我的委托正在将字符串传递给它的委托(模拟)
标签: ios objective-c testing mocking kiwi