2009-10-31 52 views
2

我使用OCMock試圖測試NSURLConnection的行爲這裏是不完整的測試:使用OCMock期待的分類方法的產量「[NSProxy doesNotRecognizeSelector」 ...]」

#include "GTMSenTestCase.h" 
#import <OCMock/OCMock.h> 

@interface HttpTest : GTMTestCase 

- (void)testShouldConnect; 

@end 

@implementation HttpTest 

- (void)testShouldConnect { 
    id mock = [OCMockObject mockForClass:[NSURLConnection class]]; 

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:mock startImmediately:NO]; 

    [[mock expect] connection:connection didReceiveResponse:OCMOCK_ANY]; 
} 

@end 

當嘲諷帶班分類方法,其中委託方法連接:didReceiveresponse:是,我得到的錯誤:

Unknown.m:0:0 Unknown.m:0: error: -[HttpTest testShouldConnect] : *** -[NSProxy doesNotRecognizeSelector:connection:didReceiveResponse:] called! 

有沒有人有這個問題

回答

4

看起來您已經創建了NSURLConnection的模擬對象。但是,NSProxy警告是正確的,NSURLConnection對象沒有選擇器連接:didReceiveResponse: - 這是一個傳遞給實現協議的對象的選擇器。

你需要模擬一個實現NSURLConnectionDelegate的對象。由於委託協議指定連接:didReceiveResponse:你不應該得到一個錯誤:)

我沒有太多的經驗與OCMock但這似乎刪除編譯錯誤:

@interface ConnectionDelegate : NSObject { } 
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
@end 

@implementation ConnectionDelegate 
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } 
@end 



@interface ConnectionTestCase : SenTestCase { } 
@end 

@implementation ConnectionTestCase 

- (void)testShouldConnect { 
id mock = [OCMockObject mockForClass:[ConnectionDelegate class]]; 

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:mock startImmediately:NO]; 

[[mock expect] connection:connection didReceiveResponse:OCMOCK_ANY]; 
} 

@end 

希望這有助於

山姆

0

我就遇到了這個錯誤,當一個項目庫正在與海灣合作委員會選項COPY_PHASE_STRIP集編譯爲YES,使符號是不可見的。測試然後對該庫運行,並且看不到需要被剔除的方法設置COPY_PHASE_STRIP=NO修復問題