使用 OCMock 期望类别方法会产生“[NSProxy doesNotRecognizeSelector”...]”

发布于 2024-08-10 09:48:31 字数 1003 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将军与妓 2024-08-17 09:48:32

看起来您已经创建了 NSURLConnection 的模拟对象。但是, NSProxy 警告是正确的, NSURLConnection 对象没有选择器 connection: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

希望这有帮助,

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 :

@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

Hope this helps,

Sam

伪心 2024-08-17 09:48:32

当使用 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 to YES so the symbols were not visible. The tests then ran against that library and could not see the methods that needed to be stubbed setting COPY_PHASE_STRIP=NO fixed the issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文