1

My problem is to test if my get service is working or not? The technique I want to use asynchronous test in Quick/Nimble (Swift)

I wonder how to set and get variable of test class? and how to test it? (async)

As you see in the code t_items is variable of test class.When I test it it FAILS.

But when I test the t_items variable in my service (production class) It PASSES I set this t_items var in production class but I don't need & want it.

Why would't I get my data returned by block? What do I miss?

Thanks

it("getlist fetches data") {

    var service = Service()

    dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
        service.getList("Test", success: { (data) -> Void in

            t_items = data as? NSMutableArray // t_times declared in before each{}

        }, error: { (error) -> Void in

        })
    })

    expect(t_items.count).toEventually(equal(3), timeout:3) // error FAILS
    expect(service.t_items).toEventually(equal(3), timeout: 3)// PASSES

}
Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
selcuk
  • 73
  • 1
  • 10
  • Besides your actual issue, you also have a classic "data race", too: the variable `t_times` will be accessed (modified) on a different thread than the `expect` statement will do. `expect` will (presumably) access the variable on the _main thread_, while your completion handler will run on some arbitrary other _non-main thread_. Unfortunately, `expect` does not magically solve this for you. You need to ensure that statement `t_items = data as? NSMutableArray` will be set on the same thread where `expect` reads this variabel, that is the main thread. – CouchDeveloper Jun 16 '15 at 10:45
  • I have have a similar issue: I put the **expect()** call inside the callback, and it always passes. I do **expect(data) == 3; expect(data) == 7** and it passes... – Christopher Francisco Jul 03 '15 at 17:29

0 Answers0