Welcome to Part 2 of this review of the Pluralsight course Automated ASP.NET MVC Testing: End to End by Jason Roberts.
With over 15 years industry experience, Jason is a Microsoft .NET MVP who has has written multiple books including Clean C#, and C# Tips. He is also an open source contributor and the creator of FeatureToggle.
Also in this series:
Testing the Model
Testing Frameworks and Test Runners
Jason begins with an overview of the testing frameworks and runners in general, explaining how they fit into the testing picture and their relationship with our test code.
In this course, Jason uses the built in Visual Studio test runner, and the NUnit testing framework.
Introduction to NUnit
NUnit is easy to install with NuGet:
PM> Install-Package NUnit
Jason introduces the most important NUnit attributes:
Next Jason explains how the Assertions work, including:
Above links point to the official documentation.
To get NUnit working with Visual Studio, install the NUnit Test Adapter. You can do this from Visual Studio: Tools -> Extensions and Updates and search for NUnit.
Writing the First Unit Tests
We see how to write a unit test for our CheckCreditHistory method.
Our 1st test is called ShouldRecognizePeopleWithBadCredit and it asserts isCreditWorth is false.
Our 2nd test is called ShouldRecognizePeopleWithGoodCredit and it asserts isCreditWorth is true.
Writing Unit Tests with Collaborators: Classical Style
We have an interface ICreditHistoryChecker with an implementation CreditHistoryChecker.
In this demonstration we use the real CreditHistoryChecker in our unit tests.
We need to specify data that the CreditHistoryChecker needs in our arrange section of our test.
Writing Unit Tests with Collaborators: Mockist Style
We install a mocking framework (Moq) and refactor our test to use a fake collaborator.
The amount of code involved is about the same. One style of unit testing is not necessarily better than the other. So it is important to practice both styles so you can pick the best option for the given situation.
Writing Integration Tests
We create a new integration test project, create a new test class and create a new integration test:
This test saves data to the database, and takes several seconds to run, rather than the milliseconds that the unit tests take to run.