使用 OCMock 期望类别方法会产生“[NSProxy doesNotRecognizeSelector”...]”
我正在使用 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!
有人对此有问题吗?
I'm using OCMock trying to test the behavior of NSURLConnection. Here's the incomplete test:
#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
When mocking a class with category methods, which the delegate method connection:didReceiveresponse: is, I get the error:
Unknown.m:0:0 Unknown.m:0: error: -[HttpTest testShouldConnect] : *** -[NSProxy doesNotRecognizeSelector:connection:didReceiveResponse:] called!
Has anyone had a problem with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您已经创建了 NSURLConnection 的模拟对象。但是, NSProxy 警告是正确的, NSURLConnection 对象没有选择器 connection:didReceiveResponse: - 这是传递给实现协议的对象的选择器。
您需要模拟一个实现 NSURLConnectionDelegate 的对象。由于委托协议指定了 connection:didReceiveResponse: 你不应该收到错误:)
我对 OCMock 没有太多经验,但这似乎消除了编译错误:
希望这有帮助,
Sam
It looks like you have created a mock object of NSURLConnection. However, the NSProxy warning is correct, an NSURLConnection object does not have the selector connection:didReceiveResponse: - that's a selector that's passed to an object that implements the protocol .
You need to mock an object that implements NSURLConnectionDelegate. As the delegate protocol specifies connection:didReceiveResponse: you should not get an error :)
I have not had much experience with OCMock but this seems to remove the compile error :
Hope this helps,
Sam
当使用 GCC 选项 COPY_PHASE_STRIP 设置为 YES 来编译项目库时,我遇到了此错误,因此符号不可见。然后针对该库运行测试,但无法看到需要存根的方法,设置
COPY_PHASE_STRIP=NO
修复了该问题I ran into this error when a project library was being compiled with GCC option
COPY_PHASE_STRIP
set toYES
so the symbols were not visible. The tests then ran against that library and could not see the methods that needed to be stubbed settingCOPY_PHASE_STRIP=NO
fixed the issue