我经常在我的iPhone Objective-C单元测试中发现我想要一个类方法,例如NSUrlConnection的+ sendSynchronousRequest:returningResponse:error:方法.
简化示例:
- (void)testClassMock
{
id mock = [OCMockObject mockForClass:[NSURLConnection class]];
[[[mock stub] andReturn:nil] sendSynchronousRequest:nil returningResponse:nil error:nil];
}
Run Code Online (Sandbox Code Playgroud)
运行时,我得到:
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).
Run Code Online (Sandbox Code Playgroud)
我很难找到关于此的任何文档,但我认为OCMock不支持类方法.
经过很多谷歌搜索,我发现了这个提示.它有效,但非常麻烦:http: //thom.org.uk/2009/05/09/mocking-class-methods-in-objective-c/
无论如何在OCMock中这样做?或者有人会想到一个聪明的OCMock类别对象可以编写来完成这种事情吗?
需要为下面的代码编写单元测试,我想做类方法canMakePayments的mock,返回yes或no,到目前为止没有好的方法找到会员,canMakePayments是一个类方法(+),似乎所有的OCMock方法都用于实例方法( - ).
你们有任何建议或讨论将不胜感激.谢谢.
// SKPaymentQueue.h
// StoreKit
if ([SKPaymentQueue canMakePayments]){
....
}
else{
...
}
Run Code Online (Sandbox Code Playgroud)