iphone OCMockObject 并对继承自 NSURLConnection 的类进行单元测试
我想对继承自 NSURLConnection 的类的自定义 init 方法进行单元测试 - 如果我的可测试类的 init 调用 NSURLConnection 的 initWithRequest ,我该如何执行此操作?
我正在使用 OCMock,通常,我可以模拟测试类中包含的对象。对于这种继承场景,最好的方法是什么?
- (void)testInit
{
id urlRequest = [OCMockObject mockForClass:[NSURLRequest class]];
MyURLConnectionWrapper *conn = [[MyURLConnectionWrapper alloc]
initWithRequest:urlRequest delegate:self
someData:extraneousData];
}
我的类是这样实现的:
@interface MyURLConnectionWrapper : NSURLConnection {
}
- (id)initWithRequest:(NSURLRequest *)request
delegate:(id)delegate someData:(NSString *)fooData
@end
@implementation MyURLConnectionWrapper
- (id)initWithRequest:(NSURLRequest *)request
delegate:(id)delegate someData:(NSString *)fooData
{
if (self = [super initWithRequest:request delegate:delegate])
{
// do some additional work here
}
return self;
}
这是我得到的错误:
OCMockObject[NSURLRequest]:意外 调用的方法:_CFURLRequest
I want to unit test the custom init method of a class that inherits from NSURLConnection -- how would I do this if the init of my testable class invokes NSURLConnection's initWithRequest
?
I'm using OCMock and normally, I can mock objects that are contained within my test class. For this inheritance scenario, what's the best approach to do this?
- (void)testInit
{
id urlRequest = [OCMockObject mockForClass:[NSURLRequest class]];
MyURLConnectionWrapper *conn = [[MyURLConnectionWrapper alloc]
initWithRequest:urlRequest delegate:self
someData:extraneousData];
}
My class is implemented like this:
@interface MyURLConnectionWrapper : NSURLConnection {
}
- (id)initWithRequest:(NSURLRequest *)request
delegate:(id)delegate someData:(NSString *)fooData
@end
@implementation MyURLConnectionWrapper
- (id)initWithRequest:(NSURLRequest *)request
delegate:(id)delegate someData:(NSString *)fooData
{
if (self = [super initWithRequest:request delegate:delegate])
{
// do some additional work here
}
return self;
}
Here's the error I get:
OCMockObject[NSURLRequest]: unexpected
method invoked: _CFURLRequest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将
mockForClass
更改为niceMockForClass
,您将获得一个存根对象,它将默默地忽略您不关心的消息,但您仍然可以为您的调用设置期望关心:If you change
mockForClass
toniceMockForClass
, you'll get a stub object that will silently ignore messages you don't care about, but you can still set expectations for the calls you do care about:你的包装器正在调用真实的东西:[super initWithRequest ...],并且 NSURLRequest 正在调用一些你没有设置期望的方法 _CFURLRequest 。由于您正在制作包装器,因此您是在“存根”NSURLConnection,而不是“模拟”它。而且您可能不想接触真实的物体。您可以使包装器实现被测单元将调用的任何方法并在其中执行断言。由于您没有从 NSURLConnection 中创建 OCMockObject,因此您无法使用验证方法。
我对 Objective-C 的模拟还很陌生,所以我还没有模拟 NSURLConnection 的方法。
Your wrapper is calling the real thing: [super initWithRequest ...], and NSURLRequest is invoking some method _CFURLRequest for which you haven't set an expectation. Since you are making a wrapper, you are "stub"bing the NSURLConnection not "mock"ing it. And you probably don't want to engage the real object. You could make your wrapper implement any methods that your unit-under-test will call and do asserts inside them. Since you are not making a OCMockObject out of the NSURLConnection then you can't use the verify methods.
I'm still new at mocking in Objective-C so I don't yet have an approach for mocking the NSURLConnection.