如何更改 OCMock 存根的返回值?

发布于 2024-11-01 18:02:55 字数 623 浏览 2 评论 0原文

似乎我第一次在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萧瑟寒风 2024-11-08 18:02:55

当您存根一个方法时,您是说它应该始终以指定的方式运行,无论它被调用多少次。解决此问题的最简单方法是将 stub 更改为 expect

CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics expect] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[[[physics expect] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];

或者,如果您需要 stub(例如,如果该方法可能不是调用),你可以重新创建模拟:

CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[physics verify];

physics = [OCMockObject mockForClass:[Physics class]];
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];
[physics verify];

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 change stub to expect:

CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics expect] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[[[physics expect] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];

Alternatively, if you need to stub (for example if the method might not be called at all), you can just re-create the mock:

CGPoint velocity1 = CGPointMake(100, 100);
CGPoint velocity2 = CGPointZero;
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity1)] getCurrentVelocity];
[testEntity update:0.1];
[physics verify];

physics = [OCMockObject mockForClass:[Physics class]];
[[[physics stub] andReturnValue:OCMOCK_VALUE(velocity2)] getCurrentVelocity];
[testEntity update:0.1];
[physics verify];
錯遇了你 2024-11-08 18:02:55

实际上,当您stub时,如果您使用andReturnandReturnValue,您只是在设置返回值。您可以随时使用方法andDo 更改返回值。这是对 expect 的改进,您需要知道一个方法将被调用多少次。这是完成此操作的代码片段:

__weak TestClass *weakSelf = self;
[[[physics stub] andDo:^(NSInvocation *invocation) {
    NSValue *result = [NSValue valueWithCGPoint:weakSelf.currentVelocity];
    [invocation setReturnValue:&result];
}] getCurrentVelocity];

Actually when you stub you're only setting the return value in stone if you use andReturn or andReturnValue. You can use the method andDo to change the returned value whenever you want. This is a improvement over expect where you need to know how many times a method will get called. Here the code snippet to accomplish this:

__weak TestClass *weakSelf = self;
[[[physics stub] andDo:^(NSInvocation *invocation) {
    NSValue *result = [NSValue valueWithCGPoint:weakSelf.currentVelocity];
    [invocation setReturnValue:&result];
}] getCurrentVelocity];
柒七 2024-11-08 18:02:55

虽然我认为 CipherCom 有正确的答案,但我发现自己更喜欢创建一个帮助器类来返回各种值。我过去曾遇到过 NSInspiration 的问题。

@interface TestHelper : NSObject
@property (nonatomic, assign) CGPoint velocity;
- (CGPoint)getCurrentVelocity;
@end

@implementation TestHelper
- (CGPoint)getCurrentVelocity
{
    return self.velocity;
}
@end

然后在我的测试类中,我将有一个 TestHelper 的私有成员变量,并在 setUp 方法中这样做:

self.testHelper = [TestHelper new];

[[[physics stub] andCall:@selector(getCurrentVelocity) onObject:self.testHelper]
                 getCurrentVelocity]; 

这样在每个测试中我都可以设置速度到我想要的测试。

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.

@interface TestHelper : NSObject
@property (nonatomic, assign) CGPoint velocity;
- (CGPoint)getCurrentVelocity;
@end

@implementation TestHelper
- (CGPoint)getCurrentVelocity
{
    return self.velocity;
}
@end

Then in my test class I'd have a private member variable for TestHelper and in setUp method I'd do:

self.testHelper = [TestHelper new];

[[[physics stub] andCall:@selector(getCurrentVelocity) onObject:self.testHelper]
                 getCurrentVelocity]; 

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);

深空失忆 2024-11-08 18:02:55

看起来 andReturn/andReturnValue/andDo 在多次调用时都不会被覆盖。我的解决方法是向测试类添加一个属性,并使用它来控制模拟对象应返回的内容。例如,如果模拟对象上有 isAvailable 属性,我的代码将如下所示:

@interface MyTest: XCTestCase 
@property BOOL stubbedIsAvailable;
@end

@implementation MyTest

- (void)setUp {
    [OCMStub([myMockedObject isAvailable]) andDo:^(NSInvocation invocation) {
        BOOL retVal = self.stubbedIsAvailable;
        [invocation setReturnValue:&retVal];
    }
}

- (void)testBehaviourWhenIsAvailable {
    self.stubbedIsAvailable = YES;
    // test the unit
}

- (void)testBehaviourWhenIsNotAvailable {
    self.stubbedIsAvailable = NOT;
    // test the unit
}

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 an isAvailable property on a mocked object, my code would look like:

@interface MyTest: XCTestCase 
@property BOOL stubbedIsAvailable;
@end

@implementation MyTest

- (void)setUp {
    [OCMStub([myMockedObject isAvailable]) andDo:^(NSInvocation invocation) {
        BOOL retVal = self.stubbedIsAvailable;
        [invocation setReturnValue:&retVal];
    }
}

- (void)testBehaviourWhenIsAvailable {
    self.stubbedIsAvailable = YES;
    // test the unit
}

- (void)testBehaviourWhenIsNotAvailable {
    self.stubbedIsAvailable = NOT;
    // test the unit
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文