2013-07-02 35 views
0

我刚开始开发一个应用程序,它连接到this URL并检索给定货币对的汇率交换。使用Kiwi/Nocilla测试HTTP

我需要测试HTTP请求,并最终了解了Kiwi和Nocilla。但是,对于任何类型的测试我都是全新的,并且没有关于Nocilla的大量信息可以帮助我开始。

我添加所有NSURLConnectionDataDelegateNSURLConnectionDelegate和方法,以我的单一视图的应用程序的ViewController,并且被存储在@property (strong, nonatomic) NSMutableData *receivedData;从URL检索的数据。当我运行程序一切正常,但我一直没能通过我写的测试:

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'是空的。

我真的很感谢所有帮助

+0

猕猴桃测试的一般结构看起来没问题。你可以显示'stubRequest'的代码吗?你确定所有的变量('view_controller'等)都是非零吗? –

+0

感谢您的回复。 'stubRequest'是[Nocilla](https://github.com/luisobo/Nocilla)的一部分,我没有实现自己。 view_controller中的所有变量都初始化为nil,并且包含从URL接收的数据的'NSMutableData'在'connectionDidFinishLoading:connection'中初始化。但是我不知道为什么只是在测试时,这个函数或任何其他与'NSURLConnectionDelegate'或'NSURLConnectionDataDelegate'有关的相关函数都没有被调用。 –

+1

'stubRequest'的代码看起来很合理,看起来应该和Kiwi一起工作,因为它只是在'LSNocilla sharedInstance'上运行。你可以尝试单步执行调试器,或者添加一些'NSLog'语句到'beginCommunication'? –

回答

1

在评论见讨论:猕猴桃试验的整体结构看起来不错,Nocilla stubRequest函数调用似乎并没有导致响应该测试期待。

也许你可以使用andReturnRawResponse来设置预期的响应数据。这样的事情(假设我得到了Nocilla语法正确):

NSData *rawData = ... 
stubRequest(...).andReturnRawResponse(rawData); 
[view_controller beginCommunication]; 
[expectFutureValue([view_controller.receivedData bytes]) 
    shouldEventuallyBeforeTimingOutAfter(2)] equal:rawData.bytes];