One of the hardest things with testing your Swift code is getting started. The blank canvas of a fresh app can be intimidating and it’s easy to postpone writing your tests until you’ve finished writing some code.

“I’ll get to it when I’ve written some code so I know what the architecture of the app is going to look like. This will make it easier for me to add tests later.”

This often leads to a situation where you’ve written plenty of code and suddenly it becomes even harder to start writing tests because, well, your code has essentially become untestable. This isn’t because you’re a bad coder — the code was simply not written with testability in mind.

In this article, we will see how depencency injection can help you architect your apps up front and solve some of your testing issues. It will even help you refactor later on.


Introduction to Dependency Injection

The main idea to take away from dependency injection (DI) is that it is a technique in which a certain object (e.g. a view controller) can receive an object it depends on (e.g. a view model) through some sort of configuration method or property.

#swift #xcode #ios #programming #mobile

Observe UserDefaults Using Combine in Swift 5
14.45 GEEK