Question

I just started developing an app that connects to this URL and retrieves the rate exchange for a given pair of currencies.

I need to test the HTTP request and I ended up learning about Kiwi and Nocilla. However, I'm completely new to any kind of testing and there's not a lot of information about Nocilla that can help me to get started.

I added all the NSURLConnectionDataDelegate and NSURLConnectionDelegate methods to the ViewController of my single view application, and the data retrieved from the URL is stored in @property (strong, nonatomic) NSMutableData *receivedData;. When I run the program everything works as expected but I haven't been able to pass the test I wrote:

SPEC_BEGIN(URLConnectionSpec)

__block URLConnectionAppDelegate *app_delegate;
__block URLConnectionViewController *view_controller;

describe(@"URLConnection", ^{
    beforeAll(^{
        [[LSNocilla sharedInstance] start];

        app_delegate = [[UIApplication sharedApplication] delegate];
        [[app_delegate shouldNot] beNil];
        view_controller = app_delegate.viewController;
    });

    afterAll(^{
        [[LSNocilla sharedInstance] stop];
    });

    afterEach(^{        
        [[LSNocilla sharedInstance] clearStubs];
    });

    context(@"When testing", ^{
        it(@"should do something", ^{
            stubRequest(@"GET", @"http://rate-exchange.appspot.com/currency?from=USD&to=EUR&q=1");

            [view_controller beginCommunication];

            [[expectFutureValue([NSString stringWithUTF8String:[view_controller.receivedData bytes]]) shouldEventuallyBeforeTimingOutAfter(2)] equal:@"{\"to\": \"EUR\", \"rate\": 0.76610740799999999, \"from\": \"USD\", \"v\": 0.76610740799999999}"];
        });
    });
});

SPEC_END

I'm sorry for the long snippet of code.

The test always failed with this message

URLConnection_WhenTesting_ShouldDoSomething] : 'URLConnection, When testing, should do something' [FAILED], expected subject to equal "{"to": "EUR", "rate": 0.76610740799999999, "from": "USD", "v": 0.76610740799999999}", got ""

I tried changing the time to even 10 seconds hoping that the test finished too early but I got the same results. I don't know why 'receivedData' is empty.

I would really appreciate any help

Was it helpful?

Solution

See discussion in comments: the overall structure of the Kiwi test looks good, the Nocilla stubRequest function call doesn't seem to result in the response that the test is expecting.

Perhaps you could use andReturnRawResponse to set up the expected response data. Something like this (assuming I got the Nocilla syntax correct):

NSData *rawData = ...
stubRequest(...).andReturnRawResponse(rawData);
[view_controller beginCommunication];
[expectFutureValue([view_controller.receivedData bytes])
  shouldEventuallyBeforeTimingOutAfter(2)] equal:rawData.bytes];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top