CedarAsync 允许您使用 Cedar 匹配器来测试异步代码。当编写集成测试而不是单元测试时,这非常有用。(CedarAsync 只支持 Cedar 的 should 语法。)
取代
client.lastResponse should contain(@"Google");
使用
in_time(client.lastResponse) should contain(@"Google");
来强制 contain
匹配器多次检查 client.lastResponse
,直到成功或超时。
#import "CedarAsync.h"
#import "HTTPClient.h"
SPEC_BEGIN(HTTPClientSpec)
using namespace Cedar::Matchers;
describe(@"HTTPClient", ^{
__block HTTPClient *client;
beforeEach(^{
client = [[[HTTPClient alloc] init] autorelease];
});
it(@"can fetch google's homepage", ^{
// uses NSURLRequest internally
[client fetchURLString:@"http://google.com"];
// plain Cedar matcher use - does not wait
// (passes immediately since it takes sometime to fetch google)
client.lastResponse should be_nil;
// async matcher use - waits for lastResponse to contain 'Google'
in_time(client.lastResponse) should contain(@"Google");
});
});
SPEC_END
暂时更改超时和轮询间隔
CedarAsync::Timing::current_timeout = 4; // seconds
CedarAsync::Timing::current_poll = 0.3; // seconds
或者
with_timeout(10, ^{
in_time(valueThatTakesForever) should equal(@"so large...");
});
更改默认超时和轮询间隔(这些值在每个测试运行之前用于填充 current_timeout
和 current_poll
)
CedarAsync::Timing::default_timeout = 4; // seconds
CedarAsync::Timing::default_poll = 2; // seconds