Chapter 1. The goal of unit testing
Listing 1.1. A sample method partially covered by a test
Listing 1.2. Version of IsStringLong that records the last result
Chapter 2. What is a unit test?
Listing 2.1. Tests written using the classical style of unit testing
Listing 2.2. Tests written using the London style of unit testing
Chapter 3. The anatomy of a unit test
Listing 3.1. A test covering the Sum method in calculator
Listing 3.2. A single-line act section
Listing 3.3. A two-line act section
Listing 3.4. Differentiating the SUT from its dependencies
Listing 3.5. Calculator with sections separated by empty lines
Listing 3.6. Arrangement and teardown logic, shared by all tests
Listing 3.7. Extracting the initialization code into the test constructor
Listing 3.8. Extracting the common initialization code into private factory methods
Listing 3.9. Common initialization code in a base class
Listing 3.10. A test named using the rigid naming policy
Listing 3.11. A test that encompasses several facts
Listing 3.12. Two tests verifying the positive and negative scenarios
Listing 3.13. Generating complex data for the parameterized test
Chapter 4. The four pillars of a good unit test
Listing 4.1. Generating an HTML representation of a message
Listing 4.2. Verifying that MessageRenderer has the correct structure
Listing 4.3. Verifying the source code of the MessageRenderer class
Listing 4.4. Verifying the outcome that MessageRenderer produces
Chapter 5. Mocks and test fragility
Listing 5.1. Using the Mock class from a mocking library to create a mock
Listing 5.2. Using the Mock class to create a stub
Listing 5.3. Asserting an interaction with a stub
Listing 5.4. storeMock: both a mock and a stub
Listing 5.5. User class with leaking implementation details
Listing 5.6. A version of User with a well-designed API
Listing 5.7. State as an implementation detail
Listing 5.8. A domain class with an application service
Listing 5.9. Connecting the domain model with external applications
Chapter 6. Styles of unit testing
Listing 6.1. Output-based testing
Listing 6.2. State-based testing
Listing 6.3. Communication-based testing
Listing 6.4. State verification that takes up a lot of space
Listing 6.5. Using helper methods in assertions
Listing 6.6. Comment compared by value
Listing 6.7. Modification of an internal state
Listing 6.8. Initial implementation of the audit system
Listing 6.9. Injecting the filesystem explicitly via the constructor
Listing 6.10. Using the new IFileSystem interface
Listing 6.11. Checking the audit system’s behavior using a mock
Listing 6.12. The AuditManager class after refactoring
Listing 6.13. The mutable shell acting on AuditManager’s decision
Listing 6.14. Gluing together the functional core and mutable shell
Chapter 7. Refactoring toward valuable unit tests
Listing 7.1. Initial implementation of the CRM system
Listing 7.2. Application service, version 1
Listing 7.4. The new class in the domain layer
Listing 7.5. Controller after refactoring
Listing 7.6. User after refactoring
Listing 7.7. User with a new property
Listing 7.8. The controller, still stripped of all decision-making
Listing 7.9. Controller deciding whether to change the user’s email
Listing 7.10. Changing an email using the CanExecute/Execute pattern
Listing 7.11. Sends a notification even when the email has not changed
Listing 7.12. User adding an event when the email changes
Chapter 8. Why integration testing?
Listing 8.1. The user controller
Listing 8.2. The integration test
Listing 8.3. An example of logging in User
Listing 8.4. Extracting support logging into the DomainLogger class
Listing 8.5. DomainLogger as a wrapper on top of ILogger
Listing 8.6. Replacing DomainLogger in User with a domain event
Listing 8.7. Latest version of UserController
Chapter 9. Mocking best practices
Listing 9.5. Integration test targeting IBus
Listing 9.6. A spy (also known as a handwritten mock)
Chapter 10. Testing the database
Listing 10.1. Class that enables access to the database
Listing 10.3. User controller, repositories, and a transaction
Listing 10.4. User controller with Entity Framework
Listing 10.5. Integration test reusing CrmContext
Listing 10.6. Base class for integration tests
Listing 10.7. Integration test with three database contexts
Listing 10.8. A separate method that creates a user
Listing 10.9. Adding default values to the factory
Listing 10.10. Using the factory method
Listing 10.11. Decorator method
Listing 10.12. Data assertions after extracting the querying logic
Listing 10.13. Fluent interface for data assertions
Listing 10.14. Integration test after moving all technicalities out of it
Chapter 11. Unit testing anti-patterns
Listing 11.1. A class with a complex private method
Listing 11.2. Extracting the complex private method
Listing 11.3. A class with a private constructor
Listing 11.4. A class with private state
Listing 11.5. Leaking algorithm implementation
Listing 11.6. A parameterized version of the same test
Listing 11.7. Test with no domain knowledge
Listing 11.8. Logger with a Boolean switch
Listing 11.9. A test using the Boolean switch
Listing 11.10. A version without the switch
Listing 11.11. A class that calculates statistics
Listing 11.12. A controller using StatisticsCalculator
Listing 11.13. Test that mocks the concrete class
Listing 11.14. Splitting StatisticsCalculator into two classes
Listing 11.15. Controller after the refactoring
Listing 11.16. Current date and time as an ambient context
Listing 11.17. Current date and time as an explicit dependency
18.224.95.38