My iOS app (Swift 5.3) has the structure below for the API and unit testing via mocking.
This does allow to test some aspects with a mocked-up API, but it's not a complete test: When running the tests, I will still run setupInitialStateForApp
against the real API (not mocked out). Is there some way in the test that I can say: Don't initialize the app until I have mocked out the API? Or would there be a better way of writing this to avoid this problem?
MyApp.swift:
@main
struct MyApp: App {
var apiServer: ApiServer?
var dataHandler: DataHandler? // Stores the app state as an observable object
init() {
self.apiServer = apiServer()
self.dataHandler = setupInitialStateForApp(apiServer: apiServer)
}
MyAppTest.swift:
testApi() {
let apiServer = APIServer()
apiServer.session = getMockSession()
// Now testing some stuff, and everything is mocked. Good!
// But `setupInitialStateForApp` already ran,
// so I made real API calls which I want to avoid.
}