| layout | keywords | comments | title | description | author | micro_nav | page_nav | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
default |
false |
Lab - Characterization Tests |
Daunted by unfamiliar legacy code? Let's practice |
|
true |
|
You've been tasked with creating a new feature. Clearly, the code is not in the best shape. So you decide you want to do some refactoring and add automated tests. Nice! the boyscout rule - leaving things better than you found them.
Before we start creating the feature, let's create some characterization tests. Characterization tests help us learn from the system and how it's currently in production. This is extremely useful when learning what a system does and how it works. If you'd like a refresher on characterization tests, there's a really good blog post from Michael Feathers.
Become familiar with the code. Don't refactor. Let's start with a single method and be inquisitive.
Feel free to create a test project. Start experimenting and creating a characterization test to understand how it works today.
As you start learning, create some test cases to start documenting given some criteria what the expected results are.
Note: If you are new to ASP.NET Core, when you create a test project, make sure it is in .NET Core and it uses Microsoft.AspNetCore.App NuGet package
Note: Remember, don't start refactoring right away. You have running database using docker. Feel free to connect to it using your tests and expirement away.
- Do you have a good idea about the kind of refactorings you'd to do to start breaking dependencies?
- What would it take to start making this code more unit testable?
- Do you comfortable enough to start refactoring without breaking it?
Create an end-state in mind of what you would like the architecture to look like. For example, what would the code look like if it was using:
- Dependency Injection
- SOLID Principles
- Clean Architecture
Don't Forget: Run your characterization tests often to make sure your refactoring is not breaking things.
Checkout the Resources tab for useful links.
- What happens when the database goes down? Can you similate this?
- Write some characterization tests for
ProductsController.Consume - Refactor with what you're comfortable