我正在尝试熟悉Kiwi BDD测试框架。我将它与Nocilla结合使用来模拟超文本传输协议请求。这两个项目看起来都很棒,但我对它有一些困难。我有以下测试规范:
beforeAll(^{ // Occurs once
[[LSNocilla sharedInstance] start];
});
afterAll(^{ // Occurs once
[[LSNocilla sharedInstance] stop];
});
beforeEach(^{ // Occurs before each enclosed "it"
couch = [[Couch alloc]initWithDatabaseUrl:@"http://myhost/mydatabase"];
});
afterEach(^{ // Occurs after each enclosed "it"
[[LSNocilla sharedInstance] clearStubs];
});
it(@"should be initialized", ^{
[couch shouldNotBeNil];
});
context(@"GET requests", ^{
it(@"should get document by id", ^{
__block NSData *successJson = nil;
__block NSError *requestErr = nil;
stubRequest(@"GET", @"http://myhost/mydatabase/test").
withHeader(@"Accept", @"application/json").
withBody(@"{\"_id\":\"test\",\"_rev\":\"2-77f66380e1670f1876f15ebd66f4e322\",\"name\":\"nick\"");
[couch getDocumentById:@"test" success:^(NSData *json){
successJson = json;
} failure:^(NSError *error) {
requestErr = error;
}];
[[successJson shouldNot]equal:nil];
});
});很抱歉有这么长的代码片段。我想确保我给出了上下文。正如您所看到的,我正在测试一个对象的行为,该对象发出GET请求,并在“成功”块中报告结果,在“失败”块中报告错误。我有两个__block变量来存储成功和失败。目前,测试检查'success‘变量是否有值(不是nil)。此测试通过。但是,调试此测试时,似乎这两个块都没有执行过。successJson显示为空。我希望Nocilla将存根主体内容传递给成功块参数。那么我的测试是不是构造不正确呢?
谢谢!
发布于 2013-06-30 09:51:45
您的测试的总体结构看起来还不错。对于asynchronous testing,请使用以下命令:
[[expectFutureValue(theValue(successJson != nil)) shouldEventually] beTrue];使用上面的!= nil和beTrue的原因是没有可以用来测试最终nil值的shouldEventually + notBeNil组合。上面的测试将发现successJson最初为nil,因此将继续轮询该值,直到您的回调将其设为非nil。
请注意,如果您正在进行阳性测试,例如检查测试== @“successJson”,那么您可以使用更简单的形式来表示期望:
[[expectFutureValue(successJson) shouldEventually] equal:@"test"];还要注意,您可以使用shouldEventuallyBeforeTimingOutAfter(2.0)将默认超时(我认为是1秒)增加到您想要的异步期望的任何超时。
https://stackoverflow.com/questions/17385537
复制相似问题