4

我需要以下帮助:我正在为具有以下结构的客户端 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() 来检查至少有一个参数发送给委托。

无论如何定义一个函数(或objective-c块)来检查参数?

谢谢!

4

1 回答 1

2

尝试使用捕获间谍

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:/* ... */];
});
于 2013-07-10T23:51:02.530 回答