Unit Tests | raywenderlich.com

Learn the principles of a good test and how to apply them to test your network code.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4001919-how-to-make-an-app-like-instagram-in-ios/lessons/13

How can I solve the error at 15:00???

When I run the test again at that point, the test succeeds. What are you seeing?

I had the exact same issue as the video at 15:00

If you click the red X by the name of the test, to run it again, does the test then pass?

nope… the error stays though :frowning:

If you’re running with a test server, make sure the server is going. Try pulling up the url in a browser.

I set up the project totally according to the tutorial video though.
Copy pasting the code from project gives me the following screenshot.

Hmm, what is the URL I should be inputting in my browser for server.GET["/api/v1/posts"] = { _ in HttpResponse.ok(.text(JsonData.goodFeed)) }

Sorry, I wasn’t at my computer before and was trying to help from memory. Don’t worry about opening it in your browser. I would try adding a breakpoint in APIClient.publisherForRequest. See what you’re getting from the request and why it’s returning an error. If you’re not able to solve it, zip up your project and I’ll see if I can take a look.

Petstagram.zip (60.0 KB)
Uploaded, thank you so much TwT

Sorry for the delay, I’ve been traveling.

Ok, before I give you the issue, let’s go down a couple of troubleshooting paths.

  1. Try setting a breakpoint on line 46 in APIClientTests. Step into publisherForRequest and look at the URL that is being requested. Since there is no external server, this is the URL that Swifter will be serving up for the test. Double check that Swifter is setup to serve that URL.

  2. In setUpWithError, we add a notFoundHandler to the server. This helps by checking for URLs that are requested, but not found. The feedback is given through an attachment to the test. You can see this by opening the Report Navigator in the left panel and clicking on the failed test run. You should see “Added attachment named ‘Unhandled API path requested’”. Expand that and click the quick look icon (looks like an eye). That will show the text from the attachment.

Now, this gives us clues, but you’ve stumbled on a pretty tricky bug. The issue is that the test is passing the local environment to APIClient and that is where the baseUrl comes from. If you look closely, there’s a typo in that string - “ap1” instead of “api”. They’re so close it’s hard to notice. If you change the “1” to an “i”, your test will pass.

I can’t be the only one who got Cannot find 'JsonData in scope` as an error, can I? I followed the video to a T but am getting this error in PostRequestTests.swift.

Make sure JsonData is selected for the PetstagramTests target. If that is selected and your build is still giving you errors, send me a zip of the project and I’ll take a look.

Hi,
I get this error when trying to run or preview the app.
I’m currently trying to do the add comment challenge but it started on the UnitTest lesson
Not sure how to handle this. Can anyone please help?

:pray:

Can you send me a zip of your project? It may be that Swifter has changes. Give me a few days and I’ll investigate.

I’m using the final version of Episode 17 in the course material: 17-challenge-comment-challenge.
I’m new here and cannot upload more attachments. Do you have an email addresses I can send it to?

Thanks,
Michal

Ok, it looks like on Sept 28th they committed a breaking change to Swifter. The easiest fix is to change .notFound to .notFound(). If you want to make sure you’re using the same version of Swifter as I used, you can change the Swifter package dependency to 1.4.7.