Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing an NSURLConnection Delegate: Faking a Connection
    primarykey
    data
    text
    <p>I'm pretty new to Objective-C and unit testing in Xcode so please bare with me! :)</p> <p>I'm currently writing a weather application and I've created a sort of weather model that does all the calculations, fetching of data, etc. Before I created the ViewControllers, I wanted to write some unit tests for my model to ensure that everything was working properly (the weather is being fetched in the expected format, weather is refreshing correctly, etc.).</p> <p>Now I would <em>love</em> to unit test with confidence and test for equality like:</p> <pre><code>STAssertEquals([[testableModel weatherDictionary]objectForKey:@"current_conditions"], @"Sunny", @"The weather should be sunny."); </code></pre> <p>...but alas, mother nature changes so fast. Also, (and correct me on this), I don't think I can connect to the Internet while I'm unit testing(...?) (Either way, it doesn't particularly matter.)</p> <p>So, I searched on my most favorite website and I found this question: <a href="https://stackoverflow.com/questions/9908547/how-to-unit-test-a-nsurlconnection-delegate">how to unit test a NSURLConnection Delegate?</a></p> <p>It was quite informative, but now I've got a question. When I call a method that invokes an NSURLConnection delegate method, such as:</p> <pre><code>[myBeautifulWeatherModel getTheWeather]; </code></pre> <p>...how do I feed the model (i.e., myBeautifulWeatherModel, which is implementing the NSURLConnection delegate methods) data? The model is going to do some JSON parsing when it receives its data in this delegate method:</p> <pre><code>- (void)connectionDidFinishLoading:(NSURLConnection *)connection; </code></pre> <p>My first thought was to take the JSON data that Wunderground sends back and just change some of the keys so that they match expected data and feed that in. But the question is HOW would I feed in that response?</p> <p>I understand that NSURLConnection is going to call 3 required delegate methods. I feel like I need to "fake out" the following:</p> <pre><code>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; </code></pre> <p>But again, I'm a little confused as to how I feed it in and get this model to <em>think</em> its connected to the Internet.</p> <p>And before anyone actually answers my question, thank you for any help you can give! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload