1. 简单介绍
以下三个OC BDD框架相对于官方框架XCTest都具有更好的可读性。另外如今已经有了比較流行的swift BDD框架: https://github.com/railsware/Sleipnir 和 https://github.com/Quick/Quick。
- An OC RSpec-like BDD DSL
- Quick and easy set up
- Build on top of XCTest
- Excellent Xcode integration
2. Specta BDD DSL语法简单介绍
-
it(@"should do some stuff asynchronously", ^{
waitUntil(^(DoneCallback done) {
// Async example blocks need to invoke done() callback.
done();
});
});
另外能够參考开源项目 https://github.com/artsy/eigen,学习当中的OCMock API的使用,框架使用比較简单,看看就懂了,不须要多说。
以下能够看出一个OCMock基本过程:获得OCMockObject -> stub方法 -> 设置expect -> verify校验运行结果 -> 调用stopMocking
- http://ocmock.org/reference/
- http://ocmock.org/features/
- http://ocmock.org/introduction/
- http://www.archive.alexvollmer.com/posts/2010/06/28/making-fun-of-things-with-ocmock/
- http://hackazach.net/code/2014/03/03/effective-testing-with-ocmock/。翻译:http://zixun.github.io/blog/2015/04/16/iosdan-yuan-ce-shi-xi-lie-yi-ocmockchang-jian-shi-yong-fang-shi/
- http://engineering.aweber.com/improving-ios-unit-tests-with-ocmock/
return [request.URL.host isEqualToString:@"mywebservice.com"];
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
NSString* fixture = OHPathForFile(@"wsresponse.json", self.class);
return [OHHTTPStubsResponse responseWithFileAtPath:fixture
statusCode:200 headers:@{@"Content-Type":@"application/json"}];
{
NSURLRequest* request = ...
XCTestExpectation* responseArrived = [self expectationWithDescription:@"response of async request has arrived"];
__block NSData* receivedData = nil;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse* response, NSData* data, NSError* error)
{
receivedData = data;
[responseArrived fulfill];
}
];
[self waitForExpectationsWithTimeout:timeout handler:^{
// By the time we reach this code, the while loop has exited
// so the response has arrived or the test has timed out
XCTAssertNotNil(receivedData, @"Received data should not be nil");
}];
}
- Fast — 測试应该可以被常常执行
- Isolated — 測试本身不能依赖于外部因素或其它測试的结果
- Repeatable — 每次执行測试都应该产生同样的结果
- Self-verifying — 測试应该依赖于断言,不须要人为干预
- Timely — 測试应该和生产代码一同书写
- 不要測试私有方法
- 不要Stub私有方法
- 不要Stub外部库
- 正确地Stub依赖
- 不要測试构造函数
- http://www.objc.io/issues/15-testing/,(翻译:http://objccn.io/issue-15/ )
- https://github.com/artsy/eigen,很专业的APP的开源码,http://objccn.io/issue-22-2/
- <Functional Reactive Programming on iOS>: RAC + 单元測试
- http://www.jianshu.com/p/73f9d719cee4
- http://nshipster.com/unit-testing/
- http://onevcat.com/2014/02/ios-test-with-kiwi/
- http://onevcat.com/2014/05/kiwi-mock-stub-test/
- https://github.com/dblock/fui,find unused objective-c imports
- <Testing with Xcode>
- <Pro iOS Continuous Integration>