describe(@"URServiceDiscoveryWrapper", ^{
context(@"method getHomeCountryWithCompletion:", ^{
__block id mockedServiceDiscovery;
beforeEach(^{
mockedServiceDiscovery = [KWMock mockForProtocol:@protocol(AIServiceDiscoveryProtocol)];
});
it(@"should return error if ServiceDiscovery fails to return country", ^{
[[mockedServiceDiscovery should] receive:@selector(getHomeCountry:)];
KWCaptureSpy *serviceDiscoverySpy = [mockedServiceDiscovery captureArgument:@selector(getHomeCountry:) atIndex:0];
__block NSError *receivedError = nil;
URServiceDiscoveryWrapper *serviceDiscoveryWrapper = [[URServiceDiscoveryWrapper alloc] initWithServiceDiscovery:mockedServiceDiscovery];
[serviceDiscoveryWrapper getHomeCountryWithCompletion:^(NSString *countryCode, NSString *locale, NSDictionary *serviceURLs, NSError *error) {
[[countryCode should] beNil];
[[locale should] beNil];
[[serviceURLs should] beNil];
receivedError = error;
}];
void(^handler)(NSString *countryCode, NSString *sourceType, NSError *error);
handler = serviceDiscoverySpy.argument;
handler(nil, nil, [NSError errorWithDomain:@"ServiceDiscovery Domain" code:1002 userInfo:nil]);
[[expectFutureValue(receivedError) shouldNotEventuallyBeforeTimingOutAfter(3.0)] beNil];
});
上述测试的Quick Nimble框架中的等效代码是什么?我是Swift的新手并测试框架。因此,面对难以适应Quick Nimble框架。
我试过这样做:
describe("URServiceDiscoveryWrapper") {
context("method getHomeCountryWithCompletion:") {
beforeEach {
class MockedServiceDiscovery: AIServiceDiscoveryProtocol {
func getHomeCountry(_ completionHandler: @escaping (_ countryCode: String, _ sourceType: String, _ error: Error?) -> Void) {
}
}
}
it("should return error if ServiceDiscovery fails to return country") {
var receivedError = nil;
var serviceDiscoveryWrapper = URServiceDiscoveryWrapper(init: MockedServiceDiscovery)
serviceDiscoveryWrapper.getHomeCountry(withCompletion: {(_ countryCode: String?, _ locale: String?, _ serviceURLs: Dictionary <String, String>?, _ error: Error?) -> Void in
expect(countryCode).to(beNil())
expect(locale).to(beNil())
expect(serviceURLs).to(beNil())
expect(receivedError).to(equal(error))
})
}
}
}
我没有得到的是如何在Quick Nimble框架中模拟和存根,我想在Quick Nimble框架中编写用Kiwi框架编写的相同测试。有人请帮帮我。