3

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
Leo Zhang
  • 3,040
  • 3
  • 24
  • 38

1 Answers1

1

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.

Adam Sharp
  • 3,618
  • 25
  • 29
  • To clarify this bit of magic: these are called [Message Pattern expectations](https://github.com/allending/Kiwi/wiki/Expectations#message-patterns). – Mike Mertsock Jun 16 '13 at 22:41
  • No, it doesn't work. I have to run your code before [cruiser energyLevelInWarpCore:8], rather than after. – Leo Zhang Jun 16 '13 at 23:21
  • @zhangchiqing Updated the question to address what I think is the issue with your example. – Adam Sharp Jun 19 '13 at 08:26
  • @zhangchiqing Also, that's how message expectations work. You set the expectation, call the code you want to test, then Kiwi automatically verifies the expectations at the end of each example. – Adam Sharp Jun 19 '13 at 08:27