In Test Driven Development, there’s the process of Red-Green-Refactor. In the Red stage, it's all about failing tests. It's important that a unit test can correctly detect failing tests to prevent false positives. After having set up a test project, Reynald shows how to create a failing test to confirm Visual Studio's testing suite detects it.
- [Narrator] So we're ready to set up our failing test. Now, when we're writing test cases, there's usually three parts of our code that we set up: one for arranging, one for acting, one for asserting. Arranging is to set up your variables. Act is where you actually execute logic. And Assert is to test your logic to see if you got the values that you expected. Before we move any further, let's go ahead and rename this to something a little bit more appropriate. We'll call it AnnualSalaryTest, and we'll start off with the Arrange, where we'll say our SalaryCalculator is going to get a new instance of itself, SalaryCalculator.
We haven't created it yet, so the compile is going to complain. So we'll take care of that in a bit. And then for Act, we'll want to see if we get a decimal value of an annualSalary that is returned when we invoke GetAnnualSalary. And we pass in a value of 50. And we know that when we pass in a value of 50, we're supposed to get a total of 104,000.
So, we will Assert that 104,000 is equal to the annualSalary that we just got back. So let's add a new project, specifically a Class Library, by right clicking on our solution, choosing Add, New Project..., and we'll choose Visual C sharp, navigate down to Class Library, and we'll call this Calculator.
Next, let's rename Class1 to SalaryCalculator. We'll choose Yes so that it updates our class name over here. Looks good, I'll save. And in your exercise folder, I have a file here that you can actually copy the logic from. I'll just copy everything from here and place it in here for the salary calculator. And essentially it takes in an hourly wage for GetAnnualSalary method, and then it will multiply it by the hours in a year, which is equal to 2080 and it will return the value.
Now, as you see here, I have it returning zero, so it's going to be incorrect. And that's purposely to create a failing test. So, it's important to create a failing test to make sure that your unit test works when it does not get the expected value. So now that I have that project, I can go back to SalaryCalculatorTestProject. We can see Visual Studio is still complaining. And that's because we need to Add Reference to our new calculator that we just created. So let's go to Reference..., Projects, select Calculator.
I'll click OK. And now that I have the reference, you can see it here under References, Calculator, I can get focused on SalaryCalculator, hold the Control key down, and hit Period, and be able to import the namespace. Let's go ahead and save, Build, and run the test. So if I go to Test, Run, All Tests, the Test Explorer shows up and we can see that we have an AnnualSalaryTest that failed. Which is what we expect because the value that it returned of zero is not equal to 104,000.
So that's it. We've created our first failing test which is good. Up next, is updating it so that it passes.
- Understanding CLR and FCL
- Working with the .NET compiler
- Setting up Visual Studio to code with ASP.NET Core
- Creating an ASP.NET MVC 6 app
- Creating a web API
- Working with C# features
- Testing in C#
- Deploying cross-platform .NET apps