kiwi-bdd / Kiwi

Simple BDD for iOS
BSD 3-Clause "New" or "Revised" License
4.14k stars 512 forks source link

Class method stubbed with a block on class mock calls block with empty parameter array #607

Open jbelkins opened 9 years ago

jbelkins commented 9 years ago

When using stub:withBlock: to stub a class method on a class mock, the block is called with an empty parameter array.

The following method is declared in ExampleClass:

+ (void)voidClassMethodWithParameter:(NSNumber *)number;

Here is the spec:

context(@"stubbing methods on class mocks", ^{

    let(classMockForSubjectClass, ^Class{
        return [KWMock mockForClass:[ExampleClass class]];
    });

    describe(@"stubbing a class method with a block", ^{

        it(@"should run the block when called", ^{
            NSNumber * parameter = @8675309;
            NSNumber * __block passedNumber = nil;
            [classMockForSubjectClass stub:@selector(voidClassMethodWithParameter:) withBlock:^id(NSArray *params) {
                passedNumber = params[0];
                return nil;
            }];
            [classMockForSubjectClass voidClassMethodWithParameter:parameter];
            [[passedNumber should] equal:parameter];
        });
    });
});

I would expect this to pass. Instead it fails with this error:

Assertions:  'stubbing methods on class mocks, stubbing a class method with a block, should run the
block when called' [FAILED], NSRangeException "*** -[__NSArrayM objectAtIndex:]: index 0 beyond
bounds for empty array" raised

Specifically, I would expect the params array to have one element, the passed NSNumber. Instead, it appears to be empty.

Here is a simple iOS project that demonstrates this error: https://github.com/jbelkins/KiwiBugExamples

Please let me know if I am not setting up this test properly or if you cannot reproduce. If someone can confirm this is a Kiwi bug and not operator error, I will spend some time trying to fix it.

ghost commented 8 years ago

I've just run into the same issue. Any solutions?