Skip to content

Conversation

@ahmetkgunay
Copy link

Why?
Sometimes we would like to test some non-published variable values based on given time.

Example:

apiClient.fetchQuestions()
    .receive(on: scheduler)
    .sink { [weak self] completion in
        switch completion {
        case .finished: 
            print("finished fetching info")
        case .failure(let error):
           // Lets assume error message is not published value and need to be tested at certain given time.
            self?.errorMessage = error.localizedDescription 
        }
    } receiveValue: { [weak self] response in
        self?.questions = response.questions
    }
    .store(in: &cancellables)

Now with advance(to:) function we can easily verify the value of variables at given time.

 context("WHEN api call failed") {
    beforeEach {
        setupSUT {
            mockClient.fetchQuestionsEvents = [ (100, .completion(.failure(.connection))) ]
        }
   }
   it("has error message") {
        scheduler.advance(to: 50)
        expect(sut.errorMessage).to(beNil())
        scheduler.advance(to: 100)
        expect(sut.errorMessage).to(equal(NetworkingError.connection.localizedDescription))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant