The growing code base must be cleaned up regularly during test-driven development. New code can be moved from where it was convenient for passing a test to where it more logically belongs. Duplication must be removed. Object, class, module, variable and method names should clearly represent their current purpose and use, as extra functionality is added. As features are added, method bodies can get longer and other objects larger.
- [Voiceover] We are now in the refactor phase.…So our project is pretty small, there's probably not going…to be that much to refactor, I mean there isn't.…So why don't we go ahead and take a look though?…If we go to our Salary Calculator we can review the code…and we know that we're going to create…another method call Hourly Wage.…So that Hourly Wage method is going to use…the same constant that's here, hours in a year.…This is not visible right now to any other method…because it's within the GetAnnualSalary method.…
So let's move it outside, so it will become visible…to any other method.…Save and we've effectively refactored now.…Anytime you make a change when refactoring…you want to Save, Build and then Rerun your unit test…to make sure nothing broke.…So on the left hand side in your Test Explore,…if we go to AnnualSalaryTest, right click on it,…select Run Selected Test.…We see that it still passes and we're effectively done…with this portion.…
Let's go ahead and create our Hourly Wage test…and that's up in the next video.…
In this course, Reynald Adolphe explains the principles of test-driven development and shows how to apply them to two different C# workflows. First, he creates a new test-driven project. He writes the test cases before the code to drive the design of the program. In the second scenario, he writes test cases for an existing C# project, to find bugs before it goes live. Along the way, Reynald uses Microsoft's MSTest, but he also introduces other testing tools (such as the unit testing framework xUnit and the mocking framework Moq) and theories (such as the red-green-refactor mantra and SOLID principles) that make test-driven development so efficient.
- Different approaches to testing
- Using testing tools and mocking frameworks
- Creating a simple C# test-driven project
- Adding a failing test
- Updating tests to pass
- Refactoring code
- Using Moq to test data