2012-04-16 75 views
5

短版的問題:嘲諷預期爲代表

有什麼不對下面的獼猴桃/ 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:錯了..

+0

我假設它與獼猴桃的參數匹配有關。也許這場比賽是在指向對象的指針?這就是爲什麼字符串不匹配,即使它們包含相同的字符串?或者它可能無法處理非對象匹配,如int ... – Sunkas 2012-04-17 08:14:10

+0

我有類似的問題,我的代理將字符串傳遞給它的代表(模擬) – quantumpotato 2012-04-20 15:51:10

回答

6

你必須調用[[mockDelegate should] receive...之前調用connectionDidFinishLoading準備mockDelegate它是關於接收消息。

+0

現在是我編寫了這段代碼的一段時間,但我很確定你是對的。我做了一個更小的方式重新編碼,然後讓它恢復工作。可能是因爲現在的預期是前期設定的。 – Sunkas 2012-10-08 12:44:48