I exploit the Resolver dependency injection framework in my iOS undertaking and unit checks. Some checks fail intermittently when run collectively (CMD + U), however they succeed when run independently.
For instance, this is a take a look at case that fails:
ViewModel
last class DefaultSuccessViewModel: SuccessViewModel {
@LazyInjected non-public var navigator: DefaultNavigator<Vacation spot>
override func onUIEvent(_ occasion: SuccessViewIntent) {
if case .onMainTap = occasion { navigator.popToRoot() }
}
}
MockNavigator:
public class MockDefaultNavigator<Vacation spot: Hashable>: DefaultNavigator<Vacation spot> {
public non-public(set) var popToRootCallsCount = 0
override public func popToRoot() {
popToRootCallsCount += 1
tremendous.popToRoot()
}
}
Assessments:
class DefaultSuccessViewModelTests {
non-public let sut: DefaultSuccessViewModel
non-public let mockNavigator: MockDefaultNavigator<Vacation spot>
init() {
let mockNavigator = MockDefaultNavigator<Vacation spot>()
Resolver.fundamental.register { mockNavigator as DefaultNavigator<Vacation spot> }
self.mockNavigator = mockNavigator
sut = DefaultSuccessViewModel()
}
@Take a look at
func onUIEvent_onItemTap_popsToRoot() {
sut.onUIEvent(.onMainTap)
#count on(mockNavigator.popToRootCallsCount == 1)
}
}
The take a look at passes when run independently however typically fails when run with others (CMD + U).
I believe the MockDefaultNavigator
state (popToRootCallsCount) is shared throughout checks.
How can I isolate MockDefaultNavigator
between checks to keep away from shared state? Why does this concern happen?