问题描述
我正在尝试检查是否使用OCMock调用了类方法。我从OCMock网站和其他答案中搜集了新的OCMock版本(2.1)增加了对存根类方法的支持。
I am trying to check if a class method is getting invoked using OCMock. I have gathered from OCMock website and other answers on SO that the new OCMock release (2.1) adds support for stubbing class methods.
我正在尝试这样做:
DetailViewController:
DetailViewController:
+(BOOL)getBoolVal
{
return YES;
}
测试案例:
-(void) testClassMethod
{
id detailMock = [OCMockObject mockForClass:[DetailViewController class]];
[[[detailMock stub] andReturnValue:OCMOCK_VALUE((BOOL){YES})] getBoolVal:nil];
}
测试正在运行并且也在成功但即使我返回NO也会成功来自 getBoolVal
中 DetailViewController 中的方法。在保持该方法的断点时,测试执行不会停止,表明该方法未被调用。
The test is running and also succeeding but it succeeds even if I return NO instead of YES from getBoolVal
method in DetailViewController
. On keeping a breakpoint on that method, the test execution does not stop indicating that the method is not called.
如何检查类方法?
推荐答案
也许我在这里遗漏了一些东西(我知道这有点旧),但你的班级签名是 +(BOOL)getBoolVal {return YES; }
并且在您的测试中,您在 getBoolVal上调用expect:nil
Maybe I'm missing something here (and I know this is a bit old), but your class signature is +(BOOL)getBoolVal { return YES; }
and in your test, you're calling expect on getBoolVal:nil
那些不匹配,对吧?在那种情况下,你的班级模拟会说,哦,这不是我期望的签名,并试图将其传递给underyling类,我相信。请参阅。
Those don't match, right? In that case, your class mock will say, "Oh, that's not the signature I expect" and try to pass it on to the underyling class, I believe. See the forwardInvocationForClassObject
in the OCMock source.
至于为什么你得到NO(因为基础类也返回YES,这使得这个测试没有实际意义,但这是另一个问题),我不是百分百肯定,但也许这只是一个C-ism - '不确定的价值,无效,0(假/否)
As far as why you're getting NO (since the underlying class also returns YES, which makes this test kind of moot, but that's another issue), I'm not 100% sure, but maybe it's just a C-ism -- 'indeterminate value, void, 0 (false/NO)"
这篇关于使用OCMock版本2.1.1测试类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!