使用Kiwi检查模拟对象上的方法参数

时间:2013-07-10 22:27:22

标签: ios objective-c delegates mocking kiwi

我需要以下帮助:我正在为具有以下结构的客户端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块)来检查参数?

谢谢!

1 个答案:

答案 0 :(得分:2)

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