0
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];
        });

What would be the equivalent code in Quick Nimble framework for the above test? I am new to Swift and testing frameworks. Hence, facing difficulty in adapting to Quick Nimble framework.

I tried doing this:

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))
                    })
                }
            }
        }

What I am not getting is how to mock and stub in Quick Nimble framework and I want to write the same test written in Kiwi framework in Quick Nimble framework. Anyone please help me out with this.

  • 1
    Your question is Not clear.. if you are using `Quick Nimble framework` and does it in Objective C? or you are converting it into swift or what ?? – Abhishek Mitra Apr 02 '18 at 10:38
  • The first code snippet is of Kiwi framework(used for obj-c). And the second snippet is of Quick Nimble framework. I have first converted the obj-c class I am trying to test to Swift and that is the reason I am using Quick Nimble framework. @Abhishek I am still not very sure how I can mock protocols for Swift. Kiwi framework uses KWMock but I don't see anything equivalent in Quick Nimble framework. I would be grateful if anyone can provide me some insights about that as well. – Sagarika Barman Apr 03 '18 at 05:57

0 Answers0