如何在OCMock中存根类方法? [英] How to stub a class method in OCMock?

查看:118
本文介绍了如何在OCMock中存根类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现在我的iPhone Objective-C单元测试,我想stub一个类方法,例如。 NSUrlConnection的+ sendSynchronousRequest:returningResponse:error:method。

I often find in my iPhone Objective-C unit tests that I want stub out a class method, e.g. NSUrlConnection's +sendSynchronousRequest:returningResponse:error: method.

简化示例:

- (void)testClassMock
{
    id mock = [OCMockObject mockForClass:[NSURLConnection class]];
    [[[mock stub] andReturn:nil] sendSynchronousRequest:nil returningResponse:nil error:nil];
}

运行时,我得到:

Test Case '-[WorklistTest testClassMock]' started.
Unknown.m:0: error: -[WorklistTest testClassMock] : *** -[NSProxy doesNotRecognizeSelector:sendSynchronousRequest:returningResponse:error:] called!
Test Case '-[WorklistTest testClassMock]' failed (0.000 seconds).



我真的很难找到任何文档,但我认为类方法

I've had a really hard time finding any documentation on this, but I assume that class methods aren't supported by OCMock.

我经过很多Google搜索后发现了这个提示。它工作,但是很繁琐:
http://thom.org.uk/2009/05/09/mocking-class-methods-in-objective-c/

I found this tip after a lot of Googling. It works, but is very cumbersome: http://thom.org.uk/2009/05/09/mocking-class-methods-in-objective-c/

有没有反正在OCMock这样做?或者有人想到一个聪明的OCMock类别对象,可以写成完成这种事情?

Is there anyway to do this within OCMock? Or can someone think of a clever OCMock category object that could be written to accomplish this sort of thing?

推荐答案

来自世界的Ruby,我明白你想要完成什么。显然,你比我今天提前三个小时尝试做同样的事情(时区事情: - )。

Coming from the world of Ruby, I understand exactly what you're trying to accomplish. Apparently, you were literally three hours ahead of me trying to do exactly the same thing today (time zone thing? :-).

无论如何,我相信,这不是在OCMock期望的方式支持,因为存根类方法需要字面上达到类和更改其方法实现,无论何时,在哪里或谁调用该方法。这与OCMock似乎为您提供一个代理对象,您可以操作和直接操作和代替指定类的真实对象相反。

Anyway, I believe that this is not supported in the way one would desire in OCMock because stubbing a class method needs to literally reach into the class and changes its method implementation regardless of when, where, or who calls the method. This is in contrast to what OCMock seems to do which is to provide you a proxy object that you manipulate and otherwise operate on directly and in lieu of a "real" object of the specified class.

例如,看起来合理的想要stub NSURLConnection + sendSynchronousRequest:returningResponse:error:method。然而,通常在我们的代码中使用这个调用有点被掩盖,因此使它非常难以参数化和交换在NSURLConnection类的mock对象。

For example, it seems reasonable to want to stub NSURLConnection +sendSynchronousRequest:returningResponse:error: method. However, it is typical that the use of this call within our code is somewhat buried, thus making it very awkward to parameterize it and swap in a mock object for the NSURLConnection class.

因为这个原因,我认为你发现的方法swizzling方法,而不是性感,正是你想做的stub类方法。说这是非常繁琐似乎极端 - 我们如何认为它是不友好,也许不如OCMock为我们的生活方便。然而,这是一个非常简明的解决方案的问题。

For this reason, I think the "method swizzling" approach you've discovered, while not sexy, is exactly what you want to do for stubbing class methods. To say it's very cumbersome seems extreme -- how about we agree it's "inelegant" and maybe not as convenient as OCMock makes life for us. Nevertheless, it's a pretty concise solution to the problem.

这篇关于如何在OCMock中存根类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆