我正在使用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当使用类别方法模拟类时,委托方法连接:didReceiveresponse: is,我得到了错误:
Unknown.m:0:0 Unknown.m:0: error: -[HttpTest testShouldConnect] : *** -[NSProxy doesNotRecognizeSelector:connection:didReceiveResponse:] called!有人对此有意见吗?
发布于 2009-11-05 15:03:47
看起来您已经创建了一个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希望这能帮上忙
相同的
发布于 2014-03-13 17:22:35
当GCC选项COPY_PHASE_STRIP设置为YES编译项目库时,我遇到了这个错误,因此符号是不可见的。然后,测试将针对该库运行,无法看到需要短截的方法,因此COPY_PHASE_STRIP=NO修复了该问题。
https://stackoverflow.com/questions/1653948
复制相似问题