When running test_UpdateSearchResults_ParsesData()âŠ
In SearchViewControllerâs updateSearchResults() method, thereâs a fatal error when self.tableView.reloadData() is called.
In the console: fatal error: unexpectedly found nil while unwrapping an Optional value
Inspecting self.tableView in the debugger at line 103, (screenshot), shows that itâs nil
I tried stepping through the test to see if there was a particular line that was causing this, and found that doing so usually resulted in the error not occurring. The one time that it still occurred, there didnât seem to be a particular line in the test that was causing it, as though it was just a âside-effectâ?
I didnât change a thing in the HalfTunes app I downloaded from your link, and Iâve gone back over the code I typed from the tutorial and everything looks right. Anybody else see this? Thanks⊠Jerry
hi Jerry: yes, I now get the same experiences as you did; probably an Xcode 8 bug that changes the meaning of instantiateInitialViewController, so it doesnât set up the view controller properly?
anyway, the SUT for this test shouldnât be the view controller, as it isnât testing anything in the view; Iâve attached the version I demo in my URLSession video course, where the networking code is in an APIManager class. The parse test runs without the tableView error.
Hi Audrey, thanks a lot for the article. It really help me get better understanding of testing. However, I got the same problem when reloading search results. It turns out tableview didnât initialized after initializing SearchViewController from Storyboard. It seems that apple do some setups in viewâs getter. Adding below code in setUp() do the trick.
_ = controllerUnderTest.view
Here are some discussion with testing viewController.
Thanks audrey for the article. I have been looking into XC UI testing for some time and found this article very helpful. Have you tried setting userdefaults of the application which is being tested while performing xc testing?
Just a very minor correction, in BullsEyeTests setup() function, following gameUnderTest = BullEyeGame(), the line âgameUnderTest.startNewGame()â should be removed. Because in Swift, the constructor BullEyeGame() calls init() by default and it calls startNEwGame() already. Thanks again Audrey for such a good tutorial!
Thereâs a section in our Mocking Objects tutorial â Mocking Apple Framework Classes â about mocking notifications or user defaults, to test your appâs interaction with these classes.
I used the authorâs DHURLSessionMock.swift in this tutorial.
I have one doubt regarding mock networking Unit Test, In the given example above if I change the mock URL and asynchronous url in test method still the test method passes. I guess It should need to fail. Any Idea?
// In the setUp method URL is different as of
override func setUp() {
super.setUp() let url = URL(string: âhttp?media=music&entity=song&term=abbaâ)
}
// Asynchronous method URL
func test_UpdateSearchResults_ParsesData() {
// when
XCTAssertEqual(controllerUnderTest?.searchResults.count, 0, âsearchResults should be empty before the data task runsâ) let url = URL(string: â/music&entity=song&term=abbaâ)
hi Ankit: you can set up the mock response to either succeed or fail; in the exercise, youâre testing whether the app handles a correct JSON response correctly. You could also test whether the app handles error responses from the server correctly, by configuring a suitable mock response. The URL doesnât actually matter, because your test isnât accessing a real server.
Iâll note though I was getting a crash when trying to load abba json file from try? Data and noticed it wasnât included in target membership and so included it. Now itâs the reload crash. I presume because the tableView isnât initialised for some reason.
hi Sean: I saw a tweet recently, complaining that Xcode seems to be defaulting to not setting target membership, and I got caught by it once or twice while working with 9 beta.
For the nil tableView problem, see Jennyâs (jt1120) post #28 from June 30, above:
This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]