In previous lessons, we explored the fundamentals of Test-Driven Development (TDD), the Red-Green-Refactor cycle, and the setup of a testing environment. Now, we shift our focus to a key aspect of TDD: managing dependencies. Managing dependencies ensures that each unit of your application can be tested in isolation, which is crucial in TDD for maintaining code reliability and robustness.
In this lesson, we will examine how to use abstraction in Swift to manage dependencies effectively. Using simple examples, we will demonstrate how to apply the Red-Green-Refactor cycle in this context. Let’s dive in.
Dependencies in software development refer to the components or systems that a piece of code relies on to function properly. In the context of testing, dependencies can complicate unit tests because they might introduce external factors that affect the test outcomes. To ensure tests are isolated and independent, we use abstractions.
In Swift, abstractions can be achieved through protocols. By programming against these protocols, you can easily swap out implementations, making code more modular and test-friendly.
For example, consider a logger that a component uses to record actions. By abstracting the logger through a protocol, you decouple the component from a specific logging implementation. This abstraction allows you to replace the actual logger with a mock or fake when testing, thus focusing on testing the component, not its dependencies.
We'll create a simple logger abstraction using Swift to demonstrate dependency management. This abstraction will define a method log
, which our UserManager
will use:
Swift1protocol Logger { 2 func log(_ message: String) 3}
The Logger
protocol defines a single method log
that accepts a message of type String
. The simplicity of this abstraction highlights the ease of creating test stubs or mocks to simulate logging during tests without invoking an actual logging mechanism.
Next, we build a UserManager
class by using the Logger
protocol. We utilize dependency injection to pass in a logger, illustrating how to maintain independence between the UserManager
and any specific logging implementation.
Swift1class UserManager {
2 private let logger: Logger
3 private var users: [String] = []
4
5 init(logger: Logger) {
6 self.logger = logger
7 }
8
9 func addUser(_ username: String) {
10 logger.log("User \(username) added")
11 users.append(username)
12 }
13
14 func getUsers() -> [String] {
15 return users
16 }
17}
In UserManager
, the logger is injected through the initializer, which allows you to provide different implementations of Logger
— such as a fake for testing or a real logger for production.
In testing, we use several different methods to simulate dependencies without relying on complex or unavailable implementations. We'll create a FakeLogger
class to test the UserManager
.
Here's how we do it:
Swift1class FakeLogger: Logger {
2 var logs: [String] = []
3
4 func log(_ message: String) {
5 logs.append(message)
6 }
7}
8
9import XCTest
10
11class UserManagerTests: XCTestCase {
12 func testAddUserWillLog() {
13 let fakeLogger = FakeLogger()
14 let userManager = UserManager(logger: fakeLogger)
15 userManager.addUser("john")
16 XCTAssertEqual(fakeLogger.logs, ["User john added"])
17 }
18}
- Red: We start by writing tests for
UserManager
. The first test checks if a user is added correctly, while the second test verifies that logging occurs. - Green: Implement
UserManager
to pass these tests, ensuring that both the user addition and logging functionalities work as expected. - Refactor: The current implementation is effective, although always look for opportunities to improve code readability and maintainability.
In this lesson, we covered how to manage dependencies in unit testing using abstractions and dependency injection. We explored the use of fake objects to isolate components during tests. Here's a recap of the key points:
- Abstract dependencies using protocols to facilitate test isolation.
- Implement dependency injection to pass dependencies like loggers.
- Use a fake logger to simulate dependencies in unit tests.
- Always apply the TDD cycle: Red - Write a failing test, Green - Implement minimal code to pass, Refactor - Optimize the code without changing its functionality.
In the following hands-on practice sessions, you will consolidate these concepts by applying them using TDD. Continue practicing to deepen your understanding and proficiency in TDD with Swift and effective dependency management.
