将原语传递给 OCMock 的存根 [英] Passing primitives to an OCMock's stub
问题描述
我正在学习如何使用 OCMock 来测试我的 iPhone 项目,我有这个场景:一个 HeightMap 类使用 getHeightAtX:andY:
方法,以及一个 Render 类使用 HeightMap代码>.我正在尝试使用一些
HeightMap
模拟对 Render 进行单元测试.这有效:
I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY:
method, and a Render class using HeightMap
. I'm trying to unit test Render using some HeightMap
mocks. This works:
id mock = [OCMockObject mockForClass:[Chunk class]];
int h = 0;
[[[mock stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:0 andY:0];
当然,仅适用于 x=0
和 y=0
.我想使用平面"高度图进行测试.这意味着我需要做这样的事情:
Of course, works only for x=0
and y=0
. I want to test using a "flat" height map. This means I need to do something like this:
id chunk = [OCMockObject mockForClass:[Chunk class]];
int h = 0;
[[[chunk stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:[OCMArg any] andY:[OCMArg any]];
但这会引发两个编译警告:
But this raises two compilation warnings:
警告:传递 'getHeightAtX:andY:'
的参数 1 从指针生成整数,无需强制转换
warning: passing argument 1 of
'getHeightAtX:andY:'
makes integer from pointer without a cast
和运行时错误:
调用了意外的方法:'getHeightAtX:0 andY:0 stubbed: getHeightAtX:15545040 andY:15545024'
我错过了什么?我发现无法将 anyValue
传递给这个模拟.
What am I missing? I found no way to pass a anyValue
to this mock.
推荐答案
OCMock 目前不支持原始参数的松散匹配.在 OCMock 论坛上讨论了支持此的潜在更改,虽然它似乎停滞不前.
OCMock doesn't currently support loose matching of primitive arguments. There's a discussion about potential changes to support this on the OCMock forums, though it seems to have stalled.
我发现的唯一解决方案是以这样一种方式构建我的测试,即我知道将传入的原始值,尽管它远非理想.
The only solution I've found is to structure my tests in such a way that I know the primitive values that will be passed in, though it's far from ideal.
这篇关于将原语传递给 OCMock 的存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!