Kiwi,BDD行为测试框架--iOS攻城狮进阶必备技能

简介: Kiwi 是一个适用于iOS开发的行为驱动测试框架,旨在提供一个足够简单易用的BDD库.

简介

Kiwi 是一个适用于iOS开发的行为驱动测试框架,旨在提供一个足够简单易用的BDD库.

项目主页: https://github.com/kiwi-bdd/Kiwi
示例下载: https://github.com/ios122/ios122

使用Cocopods 安装

target :AmazingAppTests, :exclusive => true do
  pod 'Kiwi'
end

把 AmazingAppTests 改为你自己的工程中的Tests target的名字,比如我的是 iOS122Tests,然后更新即可:

pod update --verbose --no-repo-update

为了快速测试Kiwi是否安装成功,你可以用下面的代码替换到你的 Tests目录下已有的文件中的默认内容,然后点击Xcode导航栏 Product->Test(或者使用快捷键 cmd + u),此时如果提示你 Test Failed,点击错误提示,会在左侧第四导航栏看到类似下面的错误:

Assertions: 'Math, is pretty cool' [FAILED], expected subject to equal (KWValue) 43, got (KWValue) 42
File: MathSpec.m:9

如果不能看到上述错误信息,说明你的工程配置可能有问题,可以参考这里详细微调下: Getting Started with Kiwi 2.0

规则

Kiwi的规则由以下元素组成

#import "Kiwi.h" 导入Kiwi库.这应该在规则的文件开始处最先导入.
SPEC_BEGIN(ClassName) 和 SPEC_END 宏,用于标记 KWSpec 类的开始和结束,以及测试用例的分组声明.
registerMatchers(aNamespacePrefix) 注册所有使用指定命名空间前缀的匹配器.除了Kiwi默认的匹配器,这些匹配器也可以在当前规则中使用.
describe(aString, aBlock) 开启一个上下文环境,可包含测试用例或嵌套其他的上下文环境.
为了使一个block中使用的变量真正被改变,它需要在定义时使用 __block 修饰符.
beforeAll(aBlock) 在所有内嵌上下文或当前上下文的`itblock执行之前执行一次.
afterAll(aBlock) 在所有内嵌上下文或当前上下文的`itblock执行之后执行一次.
beforeEach(aBlock) 在所有包含的上下文环境的 itblock执行之前,均各执行一次.用于初始化指定上下文环境的代码,应该放在这里.
afterEach(aBlock) 在所有包含的上下文环境的 itblock执行之后,均各执行一次.
it(aString, aBlock) 声明一个测试用例.这里描述了对对象或行为的期望.
specify(aBlock) 声明一个没有描述的测试用例.这个常用于简单的期望.
pending(aString, aBlock) 可用于标记尚未完成的功能或用例,仅会使Xcode输出一个黄色警告.(有点TODO的赶脚)
let(subject, aBlock) 声明一个本地工具变量,这个变量会在规则内所有上下文的每个 itblock执行前,重新初始化一次.

示例.

#import "Kiwi.h"
#import "YFKiwiSample.h"

SPEC_BEGIN(SpecName)

describe(@"ClassName", ^{
    registerMatchers(@"BG"); // 注册 BGTangentMatcher, BGConvexMatcher 等.

    context(@"a state the component is in", ^{
        let(variable, ^{ // 在每个包含的 "it" 执行前执行执行一次.
            return [[YFKiwiSample alloc]init];
        });

        beforeAll(^{ // 执行一次
            NSLog(@"beforAll");
        });

        afterAll(^{ // Occurs once
            NSLog(@"afterAll");
        });

        beforeEach(^{ // 在每个包含的 "it" 执行前,都执行一次.
            NSLog(@"beforeEach");
        });

        afterEach(^{ // 在每个包含的 "it" 执行后,都执行一次.
            NSLog(@"afterEach");
        });

        it(@"should do something", ^{
            NSLog(@"should do something");
//            [[variable should] meetSomeExpectation];
        });

        specify(^{
            NSLog(@"specify");
            [[variable shouldNot] beNil];
        });

        context(@"inner context", ^{
            NSLog(@"inner context");
            it(@"does another thing", ^{
                NSLog(@"does another thing");
            });

            pending(@"等待实现的东西", ^{
                NSLog(@"等待实现的东西");
            });
        });
    });
});

SPEC_END

期望

期望,用来验证用例中的对象行为是否符合你的语气.一个期望,具有如下形式: [[subject should] someCondition:anArgument].此处 [subject should]是表达式的类型, ... someCondition:anArgument] 是匹配器的表达式.

示例:

// 可以用下面的内容替换原来的tests.m中的内容,然后cmd+u
// ;测试失败可自行解决;解决不了的,继续往下看.
#import "Kiwi.h"
#import "YFKiwiCar.h"

SPEC_BEGIN(CarSpec)

describe(@"YFKiwiCar", ^{
    it(@"A Car Rule", ^{
        id car = [YFKiwiCar new];
        [[car shouldNot] beNil];
        [[car should] beKindOfClass:[YFKiwiCar class]];
        [[car shouldNot] conformToProtocol:@protocol(NSCopying)];
        [[[car should] have:4] wheels];
        [[theValue([(YFKiwiCar *)car speed]) should] equal:theValue(42.0f)];
        [[car should] receive:@selector(changeToGear:) withArguments: theValue(3)]; 

        [car changeToGear: 3];
    });
});

SPEC_END

should 和 shouldNot

[subject should] 和 [subject shouldNot] 表达式,类似于一个接收器,用于接收一个期望匹配器.他们后面紧跟的是真实的匹配表达式,这些表达式将真正被用于计算.

默认地,主语守卫(一种机制,可以保证nil不引起崩溃)也会在[subject should ]和 [subject shouldNot]被使用时创建.给 nil 发送消息,通常不会有任何副作用.但是,你几乎不会希望:一个表达式,只是为了给某个对象传递一个无足轻重的消息,就因为对象本身是nil.也就说,向nil对象本身发送消息,并不会有任何副作用;但是在BBD里,某个要被传递消息的对象是nil,通常是非预期行为.所以,这些表达式的对象守卫机制,会将左侧无法判定为不为nil的表达式判定为 fail失败.

标量装箱

"装箱"是固定术语译法,其实即使我们iOS常说的基本类型转NSObject类型(事实如此,勿喷).

部分表达式中,匹配器表达式的参数总是NSObject对象.当将一个标量(如int整型,float浮点型等)用于需要id类型参数的地方时,应使用theValue(一个标量)宏将标量装箱.这种机制也适用于: 当一个标量需要是一个表达式的主语(主谓宾,基本语法规则,请自行脑补)时,或者一个 存根 的值需要是一个标量时.

示例:

[[theValue(1 + 1) should] equal:theValue(2)];
[[theValue(YES) shouldNot] equal:theValue(NO)];
[[theValue(20u) should] beBetween:theValue(1) and:theValue(30.0)];

YFKiwiCar * car = [YFKiwiCar new];
[[theValue(car.speed) should] beGreaterThan:theValue(40.0f)];

消息模式

在iOS中,常将调用某个实例对象的方法成为给这个对象发送了某个消息.所以"消息模式"中的"消息",更多的指的的实例对象的方法;"消息模式"也就被用来判断对象的某个方法是否会调用以及是否会按照预期的方式调用.

一些 Kiwi 匹配器支持使用消息模式的期望.消息模式部分,常被放在一个表达式的后部,就像一个将要发给主语的消息一样.

示例:

YFKiwiCar * cruiser = [[YFKiwiCar alloc]init];

[[cruiser should] receive:@selector(jumpToStarSystemWithIndex:) withArguments: theValue(3)];

[cruiser jumpToStarSystemWithIndex: 3];

期望:数值 和 数字

[[subject shouldNot] beNil]
[[subject should] beNil]
[[subject should] beIdenticalTo:(id)anObject] - 比较是否完全相同
[[subject should] equal:(id)anObject]
[[subject should] equal:(double)aValue withDelta:(double)aDelta]
[[subject should] beWithin:(id)aDistance of:(id)aValue]
[[subject should] beLessThan:(id)aValue]
[[subject should] beLessThanOrEqualTo:(id)aValue]
[[subject should] beGreaterThan:(id)aValue]
[[subject should] beGreaterThanOrEqualTo:(id)aValue]
[[subject should] beBetween:(id)aLowerEndpoint and:(id)anUpperEndpoint]
[[subject should] beInTheIntervalFrom:(id)aLowerEndpoint to:(id)anUpperEndpoint]
[[subject should] beTrue]
[[subject should] beFalse]
[[subject should] beYes]
[[subject should] beNo]
[[subject should] beZero]

期望: 子串匹配

[[subject should] containString:(NSString*)substring]
[[subject should] containString:(NSString*)substring options:(NSStringCompareOptions)options]
[[subject should] startWithString:(NSString*)prefix]
[[subject should] endWithString:(NSString*)suffix]

示例:

    [[@"Hello, world!" should] containString:@"world"];
    [[@"Hello, world!" should] containString:@"WORLD" options:NSCaseInsensitiveSearch];
    [[@"Hello, world!" should] startWithString:@"Hello,"];
    [[@"Hello, world!" should] endWithString:@"world!"];

期望: 正则表达式匹配

[[subject should] matchPattern:(NSString*)pattern]
[[subject should] matchPattern:(NSString*)pattern options:(NSRegularExpressionOptions)options]
    [[@"ababab" should] matchPattern:@"(ab)+"];
    [[@" foo " shouldNot] matchPattern:@"^foo$"];
    [[@"abABab" should] matchPattern:@"(ab)+" options:NSRegularExpressionCaseInsensitive];

期望: 数量的变化

[[theBlock(^{ ... }) should] change:^{ return (NSInteger)count; }]
[[theBlock(^{ ... }) should] change:^{ return (NSInteger)count; } by:+1]
[[theBlock(^{ ... }) should] change:^{ return (NSInteger)count; } by:-1]

示例:

    it(@"Expectations: Count changes", ^{
        NSMutableArray * array = [NSMutableArray arrayWithCapacity: 42];

        [[theBlock(^{
            [array addObject:@"foo"];
        }) should] change:^{
            return (NSInteger)[array count];
        } by:+1];

        [[theBlock(^{
            [array addObject:@"bar"];
            [array removeObject:@"foo"];
        }) shouldNot] change:^{ return (NSInteger)[array count]; }];

        [[theBlock(^{
            [array removeObject:@"bar"];
        }) should] change:^{ return (NSInteger)[array count]; } by:-1];
    });

期望: 对象测试

[[subject should] beKindOfClass:(Class)aClass]
[[subject should] beMemberOfClass:(Class)aClass]
[[subject should] conformToProtocol:(Protocol *)aProtocol]
[[subject should] respondToSelector:(SEL)aSelector]

期望: 集合

对于集合主语(即,主语是集合类型的):

[[subject should] beEmpty]
[[subject should] contain:(id)anObject]
[[subject should] containObjectsInArray:(NSArray *)anArray]
[[subject should] containObjects:(id)firstObject, ...]
[[subject should] haveCountOf:(NSUInteger)aCount]
[[subject should] haveCountOfAtLeast:(NSUInteger)aCount]
[[subject should] haveCountOfAtMost:(NSUInteger)aCount]

对于集合键(即此属性/方法名对应/返回一个集合类型的对象):

[[[subject should] have:(NSUInteger)aCount] collectionKey]
[[[subject should] haveAtLeast:(NSUInteger)aCount] collectionKey]
[[[subject should] haveAtMost:(NSUInteger)aCount] collectionKey]

如果主语是一个集合(比如 NSArray数组), coollectionKey 可以是任何东西(比如 items),只要遵循语法结构就行.否则, coollectionKey应当是一个可以发送给主语并返回集合类型数据的消息.

更进一步说: 对于集合类型的主语,coollectionKey的数量总是根据主语的集合内的元素数量, coollectionKey 本身并无实际意义.

示例:

    NSArray *array = [NSArray arrayWithObject:@"foo"];
    [[array should] have:1] item];

    Car *car = [Car car];
    [car setPassengers:[NSArray arrayWithObjects:@"Eric", "Stan", nil]];
    [[[[car passengers] should] haveAtLeast:2] items];
    [[[car should] haveAtLeast:2] passengers];

期望: 交互和消息

这些期望用于验证主语是否在从创建期望到用例结束的这段时间里接收到了某个消息(或者说对象的某个方法是否被调用).这个期望会同时存储 选择器或参数等信息,并依次来决定期望是否满足.

这些期望可用于真实或模拟的独享,但是在设置 receive 表达式时,Xcode 可能会给警告(报黄).

对参数无要求的选择器:

[[subject should] receive:(SEL)aSelector]
[[subject should] receive:(SEL)aSelector withCount:(NSUInteger)aCount]
[[subject should] receive:(SEL)aSelector withCountAtLeast:(NSUInteger)aCount]
[[subject should] receive:(SEL)aSelector withCountAtMost:(NSUInteger)aCount]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCount:(NSUInteger)aCount]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCountAtLeast:(NSUInteger)aCount]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCountAtMost:(NSUInteger)aCount]

含有指定参数的选择器:

[[subject should] receive:(SEL)aSelector withArguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector withCount:(NSUInteger)aCount arguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector withCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector withCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCount:(NSUInteger)aCount arguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCountAtLeast:(NSUInteger)aCount arguments:(id)firstArgument, ...]
[[subject should] receive:(SEL)aSelector andReturn:(id)aValue withCountAtMost:(NSUInteger)aCount arguments:(id)firstArgument, ...]

示例:

subject = [Cruiser cruiser];
[[subject should] receive:@selector(energyLevelInWarpCore:) 
    andReturn:theValue(42.0f) withCount:2 arguments:theValue(7)];
[subject energyLevelInWarpCore:7];
float energyLevel = [subject energyLevelInWarpCore:7];
[[theValue(energyLevel) should] equal:theValue(42.0f)];

注意你可以将 any()通配符用作参数.如果你只关心一个方法的部分参数的值,这回很有用:

id subject = [Robot robot];
[[subject should] receive:@selector(speak:afterDelay:whenDone:) withArguments:@"Hello world",any(),any()];
[subject speak:@"Hello world" afterDelay:3 whenDone:nil];

期望:通知

[[@"MyNotification" should] bePosted];
[[@"MyNotification" should] bePostedWithObject:(id)object];
[[@"MyNotification" should] bePostedWithUserInfo:(NSDictionary *)userInfo];
[[@"MyNotification" should] bePostedWithObject:(id)object andUserInfo:(NSDictionary *)userInfo];
[[@"MyNotification" should] bePostedEvaluatingBlock:^(NSNotification *note)block];

Example:

it(@"Notification", ^{
    [[@"自定义通知" should] bePosted];

    NSNotification *myNotification = [NSNotification notificationWithName:@"自定义通知"
                                                                   object:nil];
    [[NSNotificationCenter defaultCenter] postNotification:myNotification];
});

期望: 异步调用

[[subject shouldEventually] receive:(SEL)aSelector]
[[subject shouldEventually] receive:(SEL)aSelector withArguments:(id)firstArgument, ...]

期望: 异常

[[theBlock(^{ ... }) should] raise]
[[theBlock(^{ ... }) should] raiseWithName:]
[[theBlock(^{ ... }) should] raiseWithReason:(NSString *)aReason]
[[theBlock(^{ ... }) should] raiseWithName:(NSString *)aName reason:(NSString *)aReason]

示例:

    [[theBlock(^{
        [NSException raise:@"FooException" reason:@"Bar-ed"];
    }) should] raiseWithName:@"FooException" reason:@"Bar-ed"];

自定义匹配器

Kiwi中,自定义匹配器的最简单方式是创建KWMatcher的子类,并以适当的方式重写下面示例中的方法.

为了让你自定义的匹配器在规则中可用,你需要在规则中使用 registerMatchers(namespacePrefix)进行注册.

看下Kiwi源文件中的匹配器写法(如KWEqualMatcher等),将会使你受益匪浅.

示例:

    // Snippet from AnimalTypeMatcher.m

    #pragma mark Getting Matcher Strings

    // REQUIRED: Return an array of selector strings for the expectations this
    // matcher is used for.
    //
    // For example, this matcher handles [[subject should] beTypeOfMammal:] and
    // [[subject should] beTypeOfInsect:].
    + (NSArray *)matcherStrings {
        return [NSArray arrayWithObjects:@"beTypeOfMammal:", @"beTypeOfInsect:", nil];
    }

    #pragma mark Matching

    // REQUIRED: Evaluate the predicate here.
    // self.subject is available automatically.
    // self.otherSubject is a member variable you would have declared yourself.
    - (BOOL)evaluate {
        return [[self.subject animalType] isEqual:self.otherSubject];
    }

    #pragma mark Getting Failure Messages

    // REQUIRED: Return a custom error message for when "should" is used.
    - (NSString *)failureMessageForShould {
        return @"expected subject to be an animal or insect";
    }

    // OPTIONAL: If you don't override this, Kiwi uses -failureMessageForShould: and
    // replaces the first "to" with "not to".
    - (NSString *)failureMessageForShouldNot {
        return @"expected subject not to be an animal or insect";
    }

    #pragma mark Configuring Matchers

    // These methods should correspond to the selector strings returned in +matcherStrings.
    //
    // Use them to finish configuring your matcher so that -evaluate can be called
    // successfully later. Being a subclass of KWMatcher handles other details like
    // setting up self.subject.

    - (void)beTypeOfMammal:(id)anObject {
      self.otherSubject = anObject;
    }

    - (void)beTypeOfInsect:(id)anObject {
      self.otherSubject = anObject;
    }

模拟对象

模拟对象模拟某个类,或者遵循某个写一个.他们让你在完全功能完全实现之前,就能更好地专注于对象间的交互行为,并且能降低对象间的依赖--模拟或比避免那些运行规则时几乎很难出现的情况.

it(@"Mock", ^{
    id carMock = [YFKiwiCar mock];
    [ [carMock should] beMemberOfClass:[YFKiwiCar class]];
    [ [carMock should] receive:@selector(currentGear) andReturn:theValue(3)];
    [ [theValue([carMock currentGear]) should] equal:theValue(3)];

    id carNullMock = [YFKiwiCar nullMock];
    [ [theValue([carNullMock currentGear]) should] equal:theValue(0)];
    [carNullMock applyBrakes];

    id flyerMock = [KWMock mockForProtocol:@protocol(YFKiwiFlyingMachine)];
    [ [flyerMock should] conformToProtocol:@protocol(YFKiwiFlyingMachine)];
    [flyerMock stub:@selector(dragCoefficient) andReturn:theValue(17.0f)];

    id flyerNullMock = [KWMock nullMockForProtocol:@protocol(YFKiwiFlyingMachine)];
    [flyerNullMock takeOff];
});

模拟 Null 对象

通常模拟对象收到一个非预期的选择器或消息模式时,会抛出异常(PS:iOS开发常见错误奔溃之一).在模拟对象上使用 stub 或 receive期望,期望的消息会自动添加到模拟对象上,以实现对方法的模拟.

如果你不关心模拟对象如何处理其他非预期的消息,也不想在收到非预期消息时抛出异常,那就使用 null 模拟对象吧(也即 null 对象).
模拟类的实例

创建类的模拟实例(NSObject 扩展):

[SomeClass mock]
[SomeClass mockWithName:(NSString *)aName]
[SomeClass nullMock]
[SomeClass nullMockWithName:(NSString *)aName]

创建类的模拟实例:

[KWMock mockForClass:(Class)aClass]
[KWMock mockWithName:(NSString *)aName forClass:(Class)aClass]
[KWMock nullMockForClass:(Class)aClass]
[KWMock nullMockWithName:(NSString *)aName forClass:(Class)aClass]

模拟协议的实例

创建遵循某协议的实例:

[KWMock mockForProtocol:(Protocol *)aProtocol]
[KWMock mockWithName:(NSString *)aName forProtocol:(Protocol *)aProtocol]
[KWMock nullMockForProtocol:(Protocol *)aProtocol]
[KWMock nullMockWithName:(NSString *)aName forProtocol:(Protocol *)aProtocol]

存根

存根,能返回指定定选择器或消息模式的封装好的请求.Kiwi中,你可以存根真实对象(包括类对象)或模拟对象的方法.没有指定返回值的存根,将会对应返回nil,0等零值.存根需要返回标量的,标量需要使用 theValue(某个标量)宏 装箱.

所有的存根都会在规范的一个例子的末尾(一个itblock)被清除.

存根选择器:

[subject stub:(SEL)aSelector]
[subject stub:(SEL)aSelector andReturn:(id)aValue]

存根消息模式:

[ [subject stub] *messagePattern*]
[ [subject stubAndReturn:(id)aValue] *messagePattern*]

示例:

    id cruiser = [Cruiser cruiser];
    [ [cruiser stubAndReturn:theValue(42.0f)] energyLevelInWarpCore:7];
    float energyLevel = [cruiser energyLevelInWarpCore:7];
    [ [theValue(energyLevel) should] equal:theValue(42.0f)];

    [Cruiser stub:@selector(classification) andReturn:@"Not a moon"];
    [ [ [Cruiser classification] should] equal:@"Not a moon"];

    id mock = [Animal mock];
    [mock stub:@selector(species) andReturn:@"P. tigris"];
    [ [mock.species should] equal:@"P. tigris"];

捕捉参数

有时,你可能想要捕捉传递给模拟对象的参数.比如,参数可能没有是一个没有很好实现 isEqual: 的对象,如果你想确认传入的参数是否是需要的,那就要单独根据某种自定义规则去验证.另外一种情况,也是最长遇到的情况,就是模拟对象接收的消息的某个参数是一个block;通常必须捕捉并执行这个block才能确认这个block的行为.

示例:

id robotMock = [KWMock nullMockForClass:[YFKiwiCar class]];
KWCaptureSpy *spy = [robotMock captureArgument:@selector(speak:afterDelay:whenDone:) atIndex:2];

[[robotMock should] receive:@selector(speak:) withArguments:@"Goodbye"];

[robotMock speak:@"Hello" afterDelay:2 whenDone:^{
    [robotMock speak:@"Goodbye"];
}];

void (^block)(void) = spy.argument;
block();

存根的内存管理问题

未来的某天,你或许需要存根alloc等法官法.这可能不是一个好主意,但是如果你坚持,Kiwi也是支持的.需要提前指出的是,这么做需要深入思考某些细节问题,比如如何管理初始化.

Kiwi 存根遵循 Objective-C 的内存管理机制.当存根将返回值写入一个对象时,如果选择器是以alloc,或new开头,或含有 copy时,retain消息将会由存根自动在对象发送前发送.

因此,调用者不需要特别处理由存根返回的对象的内存管理问题.
警告

Kiwi深度依赖Objective-C的运行时机制,包括消息转发(比如 forwardInvocation:).因为Kiwi需要预先判断出来哪些方法可以安全调用.使用Kiwi时,有一些惯例,也是你需要遵守的.

为了使情况简化和有条理,某些方法/选择器,是决不能在消息模式中使用,接收期望,或者被存根;否则它们的常规行为将会被改变.不支持使用这些控制器,而且使用后的代码的行为结果也会变的很奇怪.

在实践中,对于高质量的程序代码,你可能不需要担心这些,但是最好还是对这些有些印象.

黑名单(使用有风险):

所有不在白名单中的NSObject类方法和NSObject协议中的方法.(比如-class, -superclass, -retain, -release等.)
所有的Kiwi对象和方法.

白名单(可安全使用):

+alloc
+new
+copy
-copy
-mutableCopy
-isEqual:
-description
-hash
-init
其他任何不在NSObject类或NSobject协议中的方法.

异步测试

iOS应用经常有组件需要在后台和主线程中内容沟通.为此,Kiwi支持异步测试;因此就可以进行集成测试-一起测试多个对象.
expectFutureValue() 和 shouldEventually

为了设置异步测试,你 必须 使用 expectFutureValue 装箱,并且使用 shouldEventually 或 shouldEventuallyBeforeTimingOutAfter来验证.

shouldEventually 默认在判定为失败前等待一秒.

[[expectFutureValue(myObject) shouldEventually] beNonNil];

标量的处理

当主语中含有标量时,应该使用 expectFutureValue中使用 theValue装箱标量.例如:

[[expectFutureValue(theValue(myBool)) shouldEventually] beYes];

shouldEventuallyBeforeTimingOutAfter()

这个block默认值是2秒而不是1秒.

[[expectFutureValue(fetchedData) shouldEventuallyBeforeTimingOutAfter(2.0)] equal:@"expected response data"];

反转

也有shouldNotEventually和 shouldNotEventuallyBeforeTimingOutAfter 的变体.
一个基于LRResty的示例:

这个block会在匹配器满足或者超时(默认: 1秒)时完成.

This will block until the matcher is satisfied or it times out (default: 1s)

    context(@"Fetching service data", ^{
        it(@"should receive data within one second", ^{

            __block NSString *fetchedData = nil;

            [[LRResty client] get:@"http://www.example.com" withBlock:^(LRRestyResponse* r) {
                NSLog(@"That's it! %@", [r asString]);
                fetchedData = [r asString];
            }];

            [[expectFutureValue(fetchedData) shouldEventually] beNonNil];

        });

    });

文章转载自 开源中国社区[https://www.oschina.net]

相关文章
|
1月前
|
算法 计算机视觉 iOS开发
iOS 实时图像处理技术:Core Image 框架的应用
【4月更文挑战第8天】 在移动设备上实现高效的图像处理功能,对于提升用户体验和扩展应用程序能力至关重要。苹果公司的iOS平台提供了强大的Core Image框架,它允许开发者以高效和直观的方式执行复杂的图像处理任务。本文将深入探讨Core Image框架的关键特性,并通过实例演示如何在iOS应用中集成实时图像处理功能,不仅提高性能,同时保持了电池寿命的优化。我们将重点讨论面部识别、滤镜应用和性能优化等关键技术点,为读者提供一份全面的iOS图像处理指南。
|
4月前
|
机器学习/深度学习 PyTorch TensorFlow
是否有其他框架可以在iOS设备上进行机器学习?
是否有其他框架可以在iOS设备上进行机器学习?
21 1
|
4月前
|
存储 数据建模 iOS开发
iOS设备功能和框架: 什么是 Core Data,它在 iOS 中的作用是什么?
iOS设备功能和框架: 什么是 Core Data,它在 iOS 中的作用是什么?
34 1
|
4月前
|
定位技术 iOS开发
iOS设备功能和框架: 如何使用 Core Location 获取设备的位置信息?
iOS设备功能和框架: 如何使用 Core Location 获取设备的位置信息?
20 0
|
5月前
|
监控 前端开发 JavaScript
如何测试前端代码? 知道 BDD, TDD, Unit Test 么? 知道怎么测试你的前端工程么(mocha, sinon, jasmin, qUnit..)?
如何测试前端代码? 知道 BDD, TDD, Unit Test 么? 知道怎么测试你的前端工程么(mocha, sinon, jasmin, qUnit..)?
76 0
|
4天前
|
JSON 监控 测试技术
Groovy脚本编写员工上网行为监控自动化测试
本文介绍了如何使用Groovy脚本创建一个自动化工具来监控员工的网络活动。通过编写简单脚本记录员工访问的网站并打印信息,可进一步扩展为将数据保存至数据库。此外,通过设定定时任务,实现了每30分钟自动监控一次的功能。最后,展示了如何将监控数据转换为JSON格式并使用HTTP POST请求提交到网站,以实现数据的自动化上报,有助于企业保障网络安全、保护数据并提升工作效率。
41 5
|
14天前
|
测试技术 开发者
【专栏】测试驱动开发(TDD)和行为驱动开发(BDD)的核心概念与实践
【4月更文挑战第27天】本文探讨了测试驱动开发(TDD)和行为驱动开发(BDD)的核心概念与实践。TDD强调先写测试用例,通过测试推动设计,确保代码质量与可维护性。BDD侧重软件行为和业务价值,提倡使用通用语言描述行为,减少沟通障碍。选择TDD或BDD取决于项目复杂性、团队技能和业务需求。理解两者差异有助于团队做出合适的选择,发挥测试的最大价值。
|
5月前
|
监控 前端开发 JavaScript
如何测试前端代码? 知道 BDD, TDD, Unit Test 么? 知道怎么测试你的前端工程么(mocha, sinon, jasmin, qUnit..)?
如何测试前端代码? 知道 BDD, TDD, Unit Test 么? 知道怎么测试你的前端工程么(mocha, sinon, jasmin, qUnit..)?
40 0
|
1月前
|
移动开发 网络协议 小程序
基于开源IM即时通讯框架MobileIMSDK:RainbowChat-iOS端v9.0版已发布
MobileIMSDK 是一套专门为移动端开发的开源IM即时通讯框架,超轻量级、高度提炼,一套API优雅支持 UDP 、TCP 、WebSocket 三种协议,支持 iOS、Android、H5、标准Java、小程序、Uniapp,服务端基于Netty编写。
41 0
|
3月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
213 2