使用Kiwi测试AFNetworking API

时间:2013-07-15 20:58:09

标签: ios mocking afnetworking kiwi

我正在为使用kiwi的网络API编写一些测试。

以下是相关代码:

/////////////////////////////////////////////// /////// MyAPI.h ////////////////////////////////////////////////// ////

@protocol MyAPIDelegate<NSObject>

-(void) onMyMethodCall:(id)response;
-(void) onFail:(NSError*)error message:(NSString*)message;

@end

@interface MyAPI : NSObject

@property (nonatomic, weak) id<MyAPIDelegate> delegate;

-(void) myMethodCall;

@end

/////////////////////////////////////////////// /////// MyAPI.m ////////////////////////////////////////////////// ////

@synthesize delegate    

-(void) myMethodCall
{
  NSDictionary *params = [Dictionary dictionaryWithObject:@"value1" forKey:@"param1"];
  [self apiCallWithServerPath:@"logic/myMethodCall"
                   parameters:params
                    onSuccess:^(id response) {
                        [delegate onMyMethodCall];
                    }
                    onFailure:^(NSError *error, NSString* msg) {
                        [delegate onFail:error message:msg];
                    }];
}

-(void) apiCallWithServerPath:(NSString *)serverPath parameters:(NSDictionary *)parameters onSuccess:(void (^)(id))success onFailure:(void (^)(NSError *, NSString *))failure
{
    AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL urlWithString:@"http://www.myserver.com/"]];
    [client setParameterEncoding:AFFormURLParameterEncoding];

    [client postPath:serverPath
          parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
               id response = [[JSONDecoder decoder] objectWithData:responseObject];
               success(response);
             }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"API call failed:  error message = %@",[error localizedDescription]);
                failure(error, [error localizedRecoverySuggestion]);
             }];
}

/////////////////////////////////////////////// /////// MyAPITest.m ////////////////////////////////////////////////// ////

SPEC_BEGIN(MyAPITest)

describe(@"MyAPITest should", ^{
    __block MyAPI *api;
    __block MyAPI *delegateMock;

    beforeEach(^{
        delegateMock = [KWMock mockForProtocol:@protocol(MyAPIDelegate)];
        api = [[MyAPI alloc] init];
        api.delegate = delegateMock;
    });

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

    it(@"should go to the server and get an answer, dont care about the value atm", ^{
        [[api should] receive:@selector(myMethodCall)];

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

        // I usually put a breakpoint here... 
        [api myMethodCall];

        [[expectFutureValue(spy.argument) shouldEventually] beNonNil];
    });
});

SPEC_END

这是一个非常简单的测试,我正在逐步建立。最后我想测试spy.argument中的值,但是现在我只想确保它不是零。

运行测试始终失败,并显示:[FAILED]尚未捕获所请求的参数。

调试不起作用:当我尝试在测试中放置断点时(评论说明),永远不会进入方法。

同时,如果我将NSLog放入MyAPI的myMethodCall中,它们不会在控制台上打印。

非常感谢任何帮助。

///////////////////////更新/////////////////////// ///////////

事实证明,如果我评论/删除此行(来自测试):

[[api should] receive:@selector(myMethodCall)];

测试工作正常。知道为什么这条线造成了这个问题?

0 个答案:

没有答案