iOS Kiwi 测试代码例子

#import <Kiwi/Kiwi.h>

#import "UCService.h"

#import "AppManager.h"

SPEC_BEGIN(UCServiceClass)

describe(@"UCService", ^{

     UCService *__block ucService = nil;

     AppManager *__block appManager = nil;

    beforeEach(^{

       ucService = [[UCService alloc] init];

    });

    beforeAll(^{

        appManager = [AppManager manager];

    });

      context(@"test requests", ^{

        it(@"when test request register image", ^{

            [[ucService shouldNot] beNil];

            RACSignal *signal = [ucService getRegisterImageAuthCode];

            [[signal shouldNot] beNil];

            __block ResponseModel *result = nil;

            [signal subscribeNext:^(id x) {

                result = x;

            }];

            [[expectFutureValue(result) shouldEventually] beKindOfClass:[ResponseModel class]];

            [[expectFutureValue(result.body) shouldEventually] beNonNil];

            [[expectFutureValue(result.body.allKeys) shouldEventually] contain:@"code"];

        });

        context(@"modify password", ^{

            ResponseModel *__block responseModel = nil;

            it(@"when test modify password", ^{

                [appManager clearUserInfo:^(BOOL completed) {

                }];

                UCService* __weak weakUCService = ucService;

                RACSignal *signal = [[ucService loginWithTel:@"17317969263" password:@"999999"] flattenMap:^RACStream *(id value) {

                    UCService* __strong strongUCService = weakUCService;

                    return [strongUCService modifyPassowrdWithOldPassowrd:@"999999"newPwd:@"999999"confirmNewPwd:@"999999"];

                }];

                [[signal shouldNot] beNil];

                [signal subscribeNext:^(id x) {

                    responseModel = x;

                }];

                [[expectFutureValue(responseModel.body) shouldEventually] beKindOfClass:[NSDictionary class]];

                [[expectFutureValue(responseModel.body) shouldEventually] beNonNil];

                [[expectFutureValue(responseModel.head.responseCode) shouldEventually] equal:@"0000"];

            });

            context(@"sub context1", ^{

                //内嵌上下文/描述会在外围上下文执行完后才执行

                it(@"when received result", ^{

                    [[responseModel.head.responseMsg shouldNot] beNil];

                });

            });

        });

        context(@"login context with right account info", ^{

            // 测试正确账号的情况

            ResponseModel *__block loginResponse = nil;

            it(@"login with telephone with ", ^{

                [appManager clearUserInfo:^(BOOL completed) { }];

                RACSignal *signal = [ucService loginWithTel:@"17317969263" password:@"999999"];

                [signal subscribeNext:^(id x) {

                    loginResponse = x;

                }];

                [[expectFutureValue(loginResponse) shouldEventually] beKindOfClass:[ResponseModel class]];

                [[expectFutureValue(loginResponse.head) shouldEventually] beNonNil];

                [[expectFutureValue(loginResponse.head.responseCode) should] equal:@"000000"];

            });

            context(@"anylize response", ^{

                // 如果response还有多个子控制状态可以在nested context中做处理

            });

        });

        context(@"login context with wrong account info", ^{

            // 测试错误账号的情况

            Error *__block loginError = nil;

            it(@"login with telephone with ", ^{

                [appManager clearUserInfo:^(BOOL completed) {

                }];

                RACSignal *signal = [ucService loginWithTel:@"17317969263" password:@"123456"];

                [signal subscribeError:^(NSError *error) {

                    loginError = (Error *)error;

                }];

                [[expectFutureValue(loginError) shouldEventually] beKindOfClass:[Error class]];

                [[expectFutureValue(loginError.errMsg) shouldNot] beNil];

                [[expectFutureValue(loginError.errCode) shouldNot] beNil];

                [[expectFutureValue(loginError.errMsg) shouldNot] beEmpty];

                [[expectFutureValue(loginError.errCode) shouldNot] beEmpty];

                [[expectFutureValue(loginError.errCode) shouldNot] equal:@"0000"];

            });

        });

    });

   });

SPEC_END

 

 

 

 

 

 

posted on 2017-03-23 15:06  码农时刻  阅读(409)  评论(0编辑  收藏  举报