Hi Ricardo,

First I’d like to share my gratitude for reading the article and the support.

I assume we’re talking about unit testing here.

In terms of testing what I do is that I create and use a stub implementation of the dependency that the test subject requires. Swift reflection support is still not that great and very limited thus mocking is not yet something I do.

Let me show you by example:

Let’s say you have a Pay button on your app that is enabled only when the order is £40 and above.

First I would separate the view state from the order validator. The view state or view model here depends on the order validator to compute whether the order has reached the minimum spend (£40)

Because you’d be using dependency injection the order validator would have to be supplied to the view model in the app target such that:

The beauty of this solution is that OrderViewModel has no notion where the validator is being created and supplied from. It only knows that it requires the validator for it to work.

Inside the app it would Swinject providing the validator. But in a test taget you can supply whatever instance that conforms to the OrderValidator protocol. Here is an example of what the test target would look like:

I hope this is what you were looking for 🙂.

Senior iOS Engineer @ Onfido. Writing weekly blogs on iOS and programming. Follow me to stay tuned!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store