tcldr / Entwine

Testing tools and utilities for Apple's Combine framework.
MIT License
445 stars 26 forks source link

Advance scheduler to specific time #36

Open ahmetkgunay opened 9 months ago

ahmetkgunay commented 9 months ago

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