# Testing in iOS - Part 15: Challenge: Adding XCTWaiter | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169>\
**Category:** 🎓 Courses & Articles\
**Created:** [January 2, 2018, 4:51pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169 "2018-01-02T16:51:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [January 2, 2018, 4:51pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/1 "2018-01-02T16:51:22Z")

</div>

In this challenge, you'll add an XCTWaiter to your unit test.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/3530-testing-in-ios/lessons/15](https://www.raywenderlich.com/3530-testing-in-ios/lessons/15)

---

<div class="post-metadata">

**Author:** ![budhead](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/budhead/32/73979_2.png) [@budhead](https://forums.kodeco.com/u/budhead)\
**Post date:** [January 23, 2018, 8:28am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/2 "2018-01-23T08:28:35Z")

</div>

Hi bdmoakley,  
Do you think it will be more clear if you use:

```
let exp = expectation(description: "Whait the http request for fetching info content to finish")

```

Outside the get closure and:

```
exp.fulfill()

```

Inside it. After that you just have to check the timeout, but the actual assertions are in the get function.  
This way seems more easy readable for me.

Full test case:

```
func testInfoLoading() {
    let url = "https://raw.githubusercontent.com/FahimF/Test/master/DogYears-Info.rtf"
    
    let exp = expectation(description: "Whait the http request for fetching info content to finish")
    
    HTTPClient.shared.get(url: url) { data, err in
        
        XCTAssertNil(err, "There was an error fetching the info content: \(err!.localizedDescription)") // Nil safety is proved by the assertion
        
        XCTAssertNotNil(data, "There was no data after loading info content")
        
        exp.fulfill()
    }
    
    let result = XCTWaiter.wait(for: [exp], timeout: 5.0)
    assert(result == XCTWaiter.Result.completed, "The loading of info content took too long")
}

```

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [January 23, 2018, 4:14pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/3 "2018-01-23T16:14:58Z")

</div>

Sure, it’s a different way of thinking about the code. It’d be interesting to run through some various iterations of the test to see how your organization may affect the test results. That said, the original code was used to demonstrate the XCTWaiter which really dictated how the code was organized.

---

<div class="post-metadata">

**Author:** ![james404](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/james404/32/79544_2.png) [@james404](https://forums.kodeco.com/u/james404)\
**Post date:** [May 8, 2018, 4:22am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/4 "2018-05-08T04:22:49Z")

</div>

Hi, Brain! Why did you use in your code this:

```
  if res == XCTWaiter.Result.completed {
    XCTAssertNotNil(resData, "No data recived from the server for InfoView content")
  } else {
    XCTAssert(false, "The call to get the URL ran into some other error")
  }

```

instead of just that:

```
XCTAssert(result == XCTWaiter.Result.completed, "No data was received from the server for InfoView content")

```

because if that statement is true:  
`res == XCTWaiter.Result.completed`

then **resData** never be **`nil`** and **XCTAssertNotNil** assertion in your code is redundant

---

<div class="post-metadata">

**Author:** ![gprasant](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gprasant/32/111488_2.png) [@gprasant](https://forums.kodeco.com/u/gprasant)\
**Post date:** [May 23, 2018, 12:39am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/5 "2018-05-23T00:39:17Z")

</div>

One more minor tweak to @budhead’s answer

```swift
    func testInfoLoading() {
      let url = "https://raw.githubusercontent.com/FahimF/Test/master/DogYears-Info.rtf"
        let exp = expectation(description: "Download Info Content for DogYears")

        HTTPClient.shared.get(url: url) {
        (data, error) in
          XCTAssertNil(error, "There was an error loading the InfoView content")
          XCTAssertNotNil(data, "No data was received from the server for InfoView content")
          exp.fulfill()
      }
      wait(for: [exp], timeout: 5.0)
    }

```

I found that

```auto
assert(result == XCTWaiter.Result.completed, "The loading of info content took too long")

```

is not required because if the expectation isn’t fulfilled, the test will fail saying **unfulfilled exception**.

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [May 29, 2018, 8:40pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/6 "2018-05-29T20:40:10Z")

</div>

Thanks for the heads up - we’ll definitely review in the next update.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 26, 2018, 1:31pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/8 "2018-07-26T13:31:30Z")

</div>

Opened on July 26, 2018, 1:31pm UTC



---

<div class="post-metadata">

**Author:** ![bbalthaz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bbalthaz/32/81494_2.png) [@bbalthaz](https://forums.kodeco.com/u/bbalthaz)\
**Post date:** [July 29, 2018, 2:43pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/9 "2018-07-29T14:43:03Z")

</div>

The approach in testInfoLoading is to basically rewrite the actual code from the application inside the test code. Thereby, not even testing the app code. This appears to test if the call works, though does not test the app itself. Let me know if I misunderstand.

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [July 31, 2018, 3:26pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/10 "2018-07-31T15:26:13Z")

</div>

Thanks for the heads up. We’ll take a look at it next time we update the course.

---

<div class="post-metadata">

**Author:** ![necrosoft](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/necrosoft/32/81494_2.png) [@necrosoft](https://forums.kodeco.com/u/necrosoft)\
**Post date:** [June 30, 2019, 10:27am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/11 "2019-06-30T10:27:38Z")

</div>

Hi bdmoakley, i did notice that test result is NOT changing when i replace let predicate = NSPredicate(format: “resData != nil”) for let predicate = NSPredicate(format: “resData == nil”). It still have passed, even when predicate assert that resData will be nil, look like predicate not work here or i do something wrong?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 9, 2019, 11:25pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/12 "2019-07-09T23:25:08Z")

</div>

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jassi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jassi/32/81494_2.png) [@jassi](https://forums.kodeco.com/u/jassi)\
**Post date:** [August 29, 2019, 12:35am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/13 "2019-08-29T00:35:41Z")

</div>

Why do you use a instance variable instead of a local variable for storing returned data.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [September 6, 2019, 6:06am UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/14 "2019-09-06T06:06:59Z")

</div>

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [September 16, 2019, 5:41pm UTC](https://forums.kodeco.com/t/testing-in-ios-part-15-challenge-adding-xctwaiter-ray-wenderlich/38169/15 "2019-09-16T17:41:00Z")

</div>

This is explained in the video.
