如何更改 OCMock 存根的返回值?
似乎我第一次在 OCMock 存根上添加 andReturnValue 时,该返回值就已确定。例如:
id physics = [OCMockObject niceMockForClass:[DynamicPhysicsComponent class]
Entity *testEntity = [Entity entityWithPhysicsComponent:physics];
CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];
在[testEntity update]中调用存根方法。但每次存根方法返回速度 1 值,所以我猜想设置方法返回值的第二次尝试不会被执行。
有没有办法在 OCMock 中做到这一点?
It seems that the first time I add andReturnValue on an OCMock stub, that return value is set in stone. For example:
id physics = [OCMockObject niceMockForClass:[DynamicPhysicsComponent class]
Entity *testEntity = [Entity entityWithPhysicsComponent:physics];
CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];
The stubbed method is called in [testEntity update]. But each time the stubbed method is returning the velocity1 value, so I guess the second attempt to set the methods return value isn't honoured.
Is there a way to do do this in OCMock?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您
存根
一个方法时,您是说它应该始终以指定的方式运行,无论它被调用多少次。解决此问题的最简单方法是将stub
更改为expect
:或者,如果您需要
stub
(例如,如果该方法可能不是调用),你可以重新创建模拟:When you
stub
a method, you're saying it should always function in the specified way, no matter how many times it's called. The easiest way to fix this is to changestub
toexpect
:Alternatively, if you need to
stub
(for example if the method might not be called at all), you can just re-create the mock:实际上,当您
stub
时,如果您使用andReturn
或andReturnValue
,您只是在设置返回值。您可以随时使用方法andDo
更改返回值。这是对expect
的改进,您需要知道一个方法将被调用多少次。这是完成此操作的代码片段:Actually when you
stub
you're only setting the return value in stone if you useandReturn
orandReturnValue
. You can use the methodandDo
to change the returned value whenever you want. This is a improvement overexpect
where you need to know how many times a method will get called. Here the code snippet to accomplish this:虽然我认为 CipherCom 有正确的答案,但我发现自己更喜欢创建一个帮助器类来返回各种值。我过去曾遇到过
NSInspiration
的问题。然后在我的测试类中,我将有一个
TestHelper
的私有成员变量,并在setUp
方法中这样做:这样在每个测试中我都可以设置速度到我想要的测试。
self.testHelper.velocity = CGPointMake(100, 200);
While I think CipherCom has the correct answer I find myself preferring to create a helper class for returning various values. I've had issues with
NSInvocation
in the past.Then in my test class I'd have a private member variable for
TestHelper
and insetUp
method I'd do:That way in each of my tests I could set the velocity to what I want for the test.
self.testHelper.velocity = CGPointMake(100, 200);
看起来
andReturn
/andReturnValue
/andDo
在多次调用时都不会被覆盖。我的解决方法是向测试类添加一个属性,并使用它来控制模拟对象应返回的内容。例如,如果模拟对象上有isAvailable
属性,我的代码将如下所示:Looks like neither
andReturn
/andReturnValue
/andDo
doesn't get overriden when called multiple times. My workaround was to add a property to the test class and use that to control what the mocked object should return. For example in case of anisAvailable
property on a mocked object, my code would look like: