2

我正在使用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!

有人遇到过这个问题吗?

4

2 回答 2

4

看起来你已经创建了一个 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

希望这可以帮助,

山姆

于 2009-11-05T15:03:47.987 回答
0

当使用 GCC 选项编译项目库时,我遇到了这个错误COPY_PHASE_STRIPYES因此符号不可见。然后测试针对该库运行,并且看不到需要存根设置的方法COPY_PHASE_STRIP=NO修复了问题

于 2014-03-13T17:22:35.617 回答