使用OCMock版本2.1.1测试类方法

编程入门 行业动态 更新时间:2024-10-24 04:35:48
本文介绍了使用OCMock版本2.1.1测试类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试检查是否使用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类,我相信。请参阅 forwardInvocationForClassObject rel = nofollow> OCMock来源。

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 forwardInvocationForClassObjectin 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测试类方法

本文发布于:2023-11-14 14:54:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:版本   测试   方法   OCMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!