iphone OCMockObject 并对继承自 NSURLConnection 的类进行单元测试

发布于 2024-08-16 15:01:33 字数 1089 浏览 3 评论 0原文

我想对继承自 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 技术交流群。

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

发布评论

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

评论(2

装纯掩盖桑 2024-08-23 15:01:33

如果将 mockForClass 更改为 niceMockForClass,您将获得一个存根对象,它将默默地忽略您不关心的消息,但您仍然可以为您的调用设置期望关心:

id urlRequest = [OCMockObject niceMockForClass:[NSURLRequest class]];

If you change mockForClass to niceMockForClass, 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:

id urlRequest = [OCMockObject niceMockForClass:[NSURLRequest class]];
我最亲爱的 2024-08-23 15:01:33

你的包装器正在调用真实的东西:[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.

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