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

Screen Shot 2021-07-17 at 9.36.32 PM

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)) }
Screen Shot 2021-07-18 at 2.27.44 AM

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?
Screen Shot 2021-10-18 at 11.33.13 AM

: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.