objective-ctestingstubkiwi

When testing Objective-C with Kiwi, how do you explicitly remove a stub?


I'm trying to test a value that I also stub like so:

context(@"when pressing the audio alert button", ^{

    context(@"when audio is currently disabled", ^{

        beforeEach(^{
            [subject.defaults stub:@selector(boolForKey:) andReturn:theValue(NO) withArguments:SHOULD_AUDIO_ALERT_KEY];
        });

        it(@"should enable audio alerts", ^{
            [subject.audioAlertButton sendActionsForControlEvents:UIControlEventTouchDown];

            //I would like to kill/remove the stub at this moment. 

            BOOL shouldAudioAlert = [subject.defaults boolForKey:SHOULD_AUDIO_ALERT_KEY];
            [[theValue(shouldAudioAlert) should] equal:theValue(YES)];
        });
    });
});

This test will always fail because of the stub, shouldAudioAlert will always be NO. As I write this question, I realize I can avoid this by improving the design of the code (this is a retro-active test, not test-first). But I'd still like to know if I'm able to kill/remove the stub explicitly.

I couldn't find the answer from the docs or by searching. Any help is appreciated!


Solution

  • As far as I know, Kiwi doesn't provide any sort of -unstub API call, so, I don't know of any way to do this. But playing hopscotch and having something stubbed briefly and then unstubbing it will lead to tests that are hard to read and maintain. What I would recommend instead is to have separate context blocks, one in which audio is enabled, and one in which it is disabled. The latter context block would have the method stubbed.

    Another option would be to stub in a more granular fashion, based on what arguments are being submitted to the method. As you can imagine, it's a completely different dimension in which to stub than stubbing in terms of time, as you have in your example.