4

I need help with the following: I'm writing some BDD tests for an client API with the following structure:

@protocol MyAPIClientDelegate <NSObject>
  -(void)myCallbackMethod:(id)response;
@end

// BEGIN: MyAPIClientSpec.h

SPEC_BEGIN(MyAPIClientSpec)

describe(@"MyAPIClientAPI ", ^{
    __block MyAPI *api = nil;
    __block id delegateMock = nil;

    beforeEach(^{
        delegateMock = [KWMock mockForProtocol:@protocol(MyAPIClientDelegate)];
        api = [MyAPI APIClientWithDelegate:delegateMock];
    });

    afterEach(^{
        delegateMock = nil;
        api = nil;
    });

    it(@"should return a JSON { result: 'ok', token: <SOME_TOKEN> }", ^{
        [[api should] receive:@selector(myMethodCall:)];

        [[[delegateMock shouldEventually] receive] myCallbackMethod:any()];

        [api myMethodCall];
    });
});

SPEC_END

As you can see in the code above, I'm using any() to check that at least there is a parameter sent to the delegate.

Is there anyway to define a function (or objective-c block) to check the parameter?

Thanks!

Adam Sharp
  • 3,618
  • 25
  • 29
dornad
  • 1,274
  • 3
  • 17
  • 36

1 Answers1

2

Try using a capture spy:

it(@"should return a JSON { result: 'ok', token: <SOME_TOKEN> }", ^{
    [[api should] receive:@selector(myMethodCall:)];

    KWCaptureSpy *spy = [delegateMock captureArgument:@selector(myCallbackMethod:) atIndex:0];

    [api myMethodCall];

    [[spy.argument should] equal:/* ... */];
});
Adam Sharp
  • 3,618
  • 25
  • 29
  • hmmm, it should be working but it seems that there's a problem when running the test. – dornad Jul 15 '13 at 20:24
  • I'll mark this as the answer and open another question where I have more details on what is going on. – dornad Jul 15 '13 at 20:24