Leo Zhang
Leo Zhang

Reputation: 3230

How to use kiwi to test the parameters of function call?

I can use the following code to test that cruiser has been called twice. But how to test that the parameter of first call is 7, and the param of second call is 8?

id cruiser = [Cruiser cruiser];
[[cruiser should] receive:@selector(energyLevelInWrapCore:) withCount:2];
[cruiser energyLevelInWarpCore:7];
[cruiser energyLevelInWarpCore:8];

And is it possible to get the parameter after the method is called? Like the following code.

id cruiser = [Cruiser cruiser];
[cruiser stub:@selector(energyLevelInWarpCore:)];
[cruiser energyLevelInWarpCore:7];
[cruiser energyLevelInWarpCore:8];
[[[[[cruiser stub] calles][1] arguments][0] should] equal:theValue(8)]; // This doesn't work

Upvotes: 3

Views: 924

Answers (1)

Adam Sharp
Adam Sharp

Reputation: 3668

Do you have a real code example? In the example you've given, you've stubbed energyLevelInWarpCore: at the top of the test, so the test is never going to fail as you're not calling into any other code. All you're really doing is exercising the test framework.

Say you had a Cruiser object that had a single instance of WarpCore. Sending Cruiser the message engage should prime the warp core, and then power it up to full speed:

describe(@"Cruiser", ^{
    describe(@"-engage", ^{
        it(@"primes the warp core then goes to full speed", ^{
            id warpCore = [WarpCore mock];
            Cruiser *enterprise = [Cruiser cruiserWithWarpCore:warpCore];

            [[[warpCore should] receive] setEnergyLevel:7];
            [[[warpCore should] receive] setEnergyLevel:8];

            [enterprise engage];
        });
    });
});

Message patterns are one way of testing arguments (you can also use receive:withArguments:). The above example demonstrates that setting two expectations for the same selector, but with different arguments, results in two unique assertions.

You can also use Capture Spies to test arguments in more complex scenarios, such as asynchronous code.

Upvotes: 1

Related Questions