Skip to content

Is there a way to change an injected (test)value after initalization? #154

Answered by mbrandonw
agrabz asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @agrabz, you can simply use withDependencies again to alter the dependency environment for a well-defined scope:

//WHEN
//would be nice if I could change the value of DataFetcher to MockDataFetcherWithError() before this line.
await withDependencies {
  $0.dataFetch = MockDataFetcherWithError()
} operation: {
  let task = Task { await sut.handleAsyncEvent(.didPullToRefresh) } //calls DataFetcher.fetchList(), should fail to test error handling logic
  await Task.yield()
  //THEN
  XCTAssert(sut.isListRefreshing == true)
  await task.value
  XCTAssert(sut.isListRefreshing == false)
  XCTAssert(sut.isListInError == true)
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@agrabz
Comment options

Answer selected by agrabz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants