短版的问题:嘲讽预期为代表
有什么不对下面的猕猴桃/ iOS的模仿期待?
长版的问题:
我想写的猕猴桃,iOS的测试一个简单的类来处理一个NSConnection连接。为了测试这个类处理来自NSConnection的回调,我发送NSConnection通常所做的委托方法。我有一个班级的代表,可以将数据发回给使用我班的人。为了测试我的类,我必须注入一个模拟委托,然后检查我所需的方法是否被调用。就这么简单:)
我对猕猴桃测试代码:
//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).
删除测试中的最后一行仍然会生成相同的错误。我想我得到的理解:withArguments:错了..
我假设它与猕猴桃的参数匹配有关。也许这场比赛是在指向对象的指针?这就是为什么字符串不匹配,即使它们包含相同的字符串?或者它可能无法处理非对象匹配,如int ... – Sunkas 2012-04-17 08:14:10
我有类似的问题,我的代理将字符串传递给它的代表(模拟) – quantumpotato 2012-04-20 15:51:10