Entwine icon indicating copy to clipboard operation
Entwine copied to clipboard

Advance scheduler to specific time

Open ahmetkgunay opened this issue 1 year ago • 0 comments

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))
    }
}

ahmetkgunay avatar Dec 04 '23 07:12 ahmetkgunay