我需要以下方面的帮助:我正在为具有以下结构的客户端API编写一些BDD测试:

@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

如您在上面的代码中看到的,我正在使用any()来检查至少有一个参数发送给委托(delegate)。

无论如何,有没有定义一个函数(或Objective-C块)来检查参数?

谢谢!

最佳答案

尝试使用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:/* ... */];
});

08-16 01:14