gpt4 book ai didi

ios - 在 Kiwi (iOS) 中 mock 代表的期望

转载 作者:可可西里 更新时间:2023-11-01 04:36:25 24 4
gpt4 key购买 nike

问题的简短版本:

以下 Kiwi/iOS 模拟期望有什么问题?

[[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil];

问题的长版:

我正在尝试在 Kiwi、iOS 中为处理 NSConnection 的简单类编写测试。为了测试该类是否处理来自 NSConnection 的回调,我向它发送了 NSConnection 通常执行的委托(delegate)方法。我在类(class)中有一个代表将数据发回给使用我的类(class)的任何人。为了测试我的类,我必须注入(inject)一个模拟委托(delegate),然后检查是否调用了我想要的方法。就这么简单:)

我的 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: 的理解是错误的..

最佳答案

您必须调用 [[mockDelegate should] receive... before 调用 connectionDidFinishLoading 来为消息准备 mockDelegate即将收到。

关于ios - 在 Kiwi (iOS) 中 mock 代表的期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com