0

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.
}

cgold
  • 4,075
  • 1
  • 12
  • 13
  • Hard to downvote such a detailed question, but, what is the issue? Is it one that you cannot "test" because you don't have access to a specific device? Or maybe one that the simulator cannot handle? Maybe it's something related to what you call "mock"? I get the feeling you are making *much* more out of testing than I would. So maybe the real question I have is - what *specifically* are you not able to "test", and how can I duplicate the issue? –  Jan 17 '21 at 17:11
  • Maybe it's me, and it's a communication issue I rarely see (at least on here). What is `an `APIServer`? What is* "mocking"*? The details in your question, while very good, don't mesh with either the questions I'm used to seeing here nor with what I think in terms of iOS testing. What am I missing? –  Jan 17 '21 at 17:14

0 Answers0