Learn what dependency injection is, and how it applies to writing tests. Mark explores the reasons why you need to use Dependency Injection in your project. He also walks through a quick example, making it easy to understand how to implement it in your project.
- [Voiceover] So we're on board with making our apps more testable, but how can we do that? Tests will be easier to write if we can separate our classes from each other and examine them individually, but that's not possible all the time. To help us with this problem, let's take a look at something called the Dependency Inversion Principle. In its essence, it serves to decouple classes from one another by explicitly providing dependencies for them, rather than having them create the dependencies themselves. Dependencies in this context refers to the other classes that a class needs in order to accomplish a task.
For example, a class making a web request might have a dependency on URLSession. Instead of having the class create the instance of a dependency, we can pass that dependency in through the initializer. Thus we are injecting the class's dependencies when we create them. So if we had, for example, a PhotoDownloader class, it might have a dependency on URLSession and URLCache, which it creates an instance of when you create an instance of the PhotoDownloader class. If we were to apply the Dependency Inversion Principle, we would flip this diagram and instead first create the URLSession and URLCache and pass it into the initializer of the PhotoDownloader.
- Why write unit tests?
- What is dependency injection?
- Using protocols to help with tests
- Handling external dependencies
- Anatomy of a test case
- Writing tests
- Analyzing code coverage
- Visualizing test results
- Writing and extending UI tests
Skill Level Intermediate
Core Data for iOS and macOS Enterprise Developerswith Jon Bott1h 46m Intermediate
Learning Server-Side Swift with Vaporwith Ron Buencamino1h 30m Intermediate
1. Dependency Injection
2. Using Protocols
3. Writing Tests
4. UI Tests
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.