Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advance scheduler to specific time #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

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