嘲笑Kiwi(iOS)对代表的期望

嘲笑Kiwi(iOS)对代表的期望,第1张

概述问题的简短版本: 以下Kiwi / iOS模拟期望有什么问题? [[mockDelegate should] receive:@selector(connectionDidSucceedWithText:andStatus:) withArguments:[testString1 stringByAppendingString:testString2],theValue(value),nil]; 问题的简短版本:

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

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

长版问题:

我正在尝试在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],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:是错误的..

解决方法 您必须在调用connectionDIDFinishLoading之前调用[[mockDelegate should] receive …以准备mockDelegate以获取它将要接收的消息. 总结

以上是内存溢出为你收集整理的嘲笑Kiwi(iOS)对代表的期望全部内容,希望文章能够帮你解决嘲笑Kiwi(iOS)对代表的期望所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071284.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存