TestStore not receiving effect when used as stored property in test classes #3052
Replies: 3 comments 4 replies
-
Hi @adityadaniel , The Here’s a more appropriate setup that aligns with the principles of isolated and exhaustive testing: final class EffectsBasicsTests: XCTestCase {
private var testStore: TestStore<EffectsBasics.State, EffectsBasics.Action>!
override func setUp() {
super.setUp()
testStore = TestStore(initialState: EffectsBasics.State()) {
EffectsBasics()
} withDependencies: {
$0.factClient.fetch = { "\($0) is a good number Brent" }
$0.continuousClock = ImmediateClock()
}
}
override func tearDown() {
testStore = nil
super.tearDown()
}
@MainActor
func testNumberFact() async {
await testStore.send(.incrementButtonTapped) {
$0.count = 1
}
await testStore.send(.numberFactButtonTapped) {
$0.isNumberFactRequestInFlight = true
}
// This would be where you expect the effect's response
// await testStore.receive(.numberFactResponse.success("1 is a good number Brent")) {
// $0.isNumberFactRequestInFlight = false
// $0.numberFact = "1 is a good number Brent"
// }
}
}
|
Beta Was this translation helpful? Give feedback.
-
@adityadaniel In general we do not recommend holding onto the test store in this fashion. @Matejkob does a good job breaking down why you're seeing the behavior you're seeing, but also we just don't think initializing the test store at the level of the test class is a good idea. In general, each test should be responsible for initializing a feature's state and dependencies for what is being tested. A lot of the time you will want to provide different initial state or dependencies depending on what you are testing, and so initializing the test store the same way each time unfortunately goes against this. We'll be adding docs for this in #3054, which also includes a helper that at least allows you to get exhaustive test coverage back in a manual way, but we highly recommend initializing test stores in each individual test, instead. |
Beta Was this translation helpful? Give feedback.
-
Tagging @jeffersonsetiawan @wendyliga @dikasetiadi for visibility |
Beta Was this translation helpful? Give feedback.
-
Hi @mbrandonw @stephencelis, My teammate found surprising behaviour when working on unit test that has asynchronous / long living effect. The test setup can be described as follow (I use Effect basic tests from SwiftUI case studies)
Instead of creating TestStore in each test cases, we create TestStore as stored property and reusing them between each test cases. If you run test case above, it will succeed. There's no test failure on not receiving effect.
However when we follow the test example by creating TestStore locally on each test case function, the test will failed on not receiving effect. Which we expect that.
Is creating TestStore as stored property not supported in TCA and is it expected behaviour? Does creating TestStore locally on each test case function the best practice on writing unit test?
Beta Was this translation helpful? Give feedback.
All reactions