我刚刚开始开发一个连接到这个 URL并检索给定货币对的汇率兑换的应用程序。
我需要测试 HTTP 请求,最终我了解了 Kiwi 和 Nocilla。但是,我对任何类型的测试都是全新的,并且没有太多关于 Nocilla 的信息可以帮助我入门。
我将所有NSURLConnectionDataDelegate
和NSURLConnectionDelegate
方法添加到ViewController
我的单视图应用程序中,从 URL 检索到的数据存储在@property (strong, nonatomic) NSMutableData *receivedData;
. 当我运行程序时,一切都按预期工作,但我无法通过我编写的测试:
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
对于长长的代码片段,我感到很抱歉。
测试总是失败并显示此消息
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 ""
我尝试将时间更改为 10 秒,希望测试完成得太早,但我得到了相同的结果。我不知道为什么“receivedData”是空的。
我真的很感激任何帮助