我刚刚开始开发一个连接到this 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‘是空的。
如果有任何帮助,我将不胜感激
发布于 2013-07-03 09:51:39
参见评论中的讨论: Kiwi测试的整体结构看起来很好,Nocilla stubRequest函数调用似乎没有得到测试预期的响应。
也许您可以使用andReturnRawResponse来设置预期的响应数据。如下所示(假设我的Nocilla语法是正确的):
NSData *rawData = ...
stubRequest(...).andReturnRawResponse(rawData);
[view_controller beginCommunication];
[expectFutureValue([view_controller.receivedData bytes])
shouldEventuallyBeforeTimingOutAfter(2)] equal:rawData.bytes];https://stackoverflow.com/questions/17423089
复制相似问题