ios – 使用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块)来检查参数?

谢谢!

最佳答案 尝试使用
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:/* ... */];
});
点赞