在Kiwi(iOS)中嘲笑对代表的期望 [英] Mocking expectations in Kiwi (iOS) for a delegate
问题描述
问题的简短版本:
以下Kiwi/iOS模拟期望出了什么问题?
What is wrong with the following Kiwi/iOS mock expectation?
[[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];
长版问题:
我正在尝试在iOS的Kiwi中为处理NSConnection的简单类编写测试.为了测试该类是否处理了来自NSConnection的回调,我向其发送了NSConnection通常执行的委托方法.我在班级中有一个委托,它将数据发送回使用我班级的任何人.要测试我的类,我必须注入一个模拟的委托,然后检查是否调用了我想要的方法.就这么简单:)
I am trying to write a test in Kiwi, iOS for a simple class that handles a NSConnection. To test that the class handles the callback from the NSConnection I send it the delegate methods NSConnection normally does. I have a delegate in the class that sends data back to whoever uses my class. To test my class I have to inject a mocked delegate and then check that my desired methods are called. Simple as that :)
我用于Kiwi测试的代码是:
My code for the Kiwi test is:
//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中:
And in my 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.仍然在测试的最后一行失败,并显示以下错误:
This code works and is correct. Debugging it with checkpoint shows it does as expected. The values of statusCode is 11111. and txt is testString1+textString2. Still it fails on the last row on in the test with the following error:
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:的理解是错误的.
Removing the last row in the test still generate the same error. I guess my understanding of receive:withArguments: is wrong..
推荐答案
您必须先调用[[mockDelegate should] receive...
,然后再调用connectionDidFinishLoading
来为要接收的消息准备模拟代理.
You have to call [[mockDelegate should] receive...
before the call to connectionDidFinishLoading
to prepare the mockDelegate for the message it's about to receive.
这篇关于在Kiwi(iOS)中嘲笑对代表的期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!