objective c - Unit testing with NSURLConnection -


I want to test a piece of code that uses the network ( NSURLConnection class , be specific ). Code (let's call it NetworkManager ) looks like this:

  - (ID) build connection {// some more code and then: return [NSURL connection. .]; } - (zero) current networking {id connection = [build build itself]; // ...}  

In unit testing, I would like to get rid of networking, that is, By replacing a NSURLConnection object. How can I do this?

I have tried to make a partial duplicate of NetworkManager which will change the buildconnection method. The problem is that partial duplicate, like That is done only by messages in the external world - sending buildConnection to startNetworkSync , pointing to the original method, not a stub.

I have also tried the monkey-pining NetworkManager class through a square. It works, I can easily override the buildConnection method by other code and can replace the actual NSURLConnection with a stub. The problem is that I have not found any easy way that I can get a stubbed connection in the test - a private part of the connection NetworkManager .

Then I override the NetworkManager , build connection method and add an instance variable and an accelerator to the built-in connection. It seems like a lot of code, though.

How do you solve it? I am looking for a solution that keeps the NetworkManager class design clean and does not require much magic in the exam nor requires many codes.

It is dependent on the dependency injection that is designed to solve; If you use startNetworkSyncWithConnection: (NSURLConnection *) , you can easily test the method with a fake connection. If you do not want to change the API for your clients, you startNetworkSync to keep it as a wrapper that does not even certain, but it [self buildconnection thereto ] with arguments to say


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -