0

I'm writing tests for my application with usage of Quick and Nimble. I've arrived at the part of the tests that require authentication. My app works in a manner that there's a singleton named AuthenticationManager that has methods for logging in etc. This class also holds the current authenticationToken:

public var token: String?

public var headers: HTTPHeaders {
    guard let token = token else {
        return ["Accept": "application/json"]
    }

    return [
        "Authorization": "Bearer \(token)",
        "Accept": "application/json"
    ]
}

This works in actual usage, however, when this runs with testing, it seems that the value of the token variable resets when my test starts. I tried making the login call in the setUp() method of my QuickSpec file, this however, didn't work. I also tried making te login call within the it statement for my test, which also didn't work. This is my code as is right now:

override func spec() {

    describe("When a user validates their current password") {
        describe("And the password is correct") {
            it("should validate the user correctly") {
                AuthenticationManager.shared.login(username: "test@example.com", password: "secret") { _ in }
                expect(Validator.validateCurrentPassword("secret")).to(equal(true))
            }
        }
    }
}

I've checked, and the login details are correct, but there's never a token available when the test runs... Does anyone have any experience with this?

ilikecode
  • 391
  • 3
  • 14
  • Possible duplicate of [Swift TDD & async URLSession - how to test?](https://stackoverflow.com/questions/40330692/swift-tdd-async-urlsession-how-to-test) – Scriptable Apr 23 '18 at 14:48
  • ^^ Logging in I presume is an async network task that takes some time to complete. you should use Expectations and do your assertions in the completionHandler once you have a valid response – Scriptable Apr 23 '18 at 14:49

0 Answers0