我正在使用OCMock来测试NSURLConnection的行为。这是不完整的测试:
#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
使用类别方法模拟类时,委托方法 connection:didReceiveresponse:是,我得到错误:
Unknown.m:0:0 Unknown.m:0: error: -[HttpTest testShouldConnect] : *** -[NSProxy doesNotRecognizeSelector:connection:didReceiveResponse:] called!
有人有这个问题吗?
答案 0 :(得分:4)
看起来你已经创建了NSURLConnection的模拟对象。但是,NSProxy警告是正确的,NSURLConnection对象没有选择器连接:didReceiveResponse: - 这是一个传递给实现协议的对象的选择器。
您需要模拟实现NSURLConnectionDelegate的对象。由于委托协议指定connection: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
希望这有帮助,
萨姆
答案 1 :(得分:0)
在使用GCC选项COPY_PHASE_STRIP
设置为YES
编译项目库时遇到此错误,因此符号不可见。然后测试针对该库运行,无法看到需要设置的方法设置COPY_PHASE_STRIP=NO
修复了问题