Automated ASP.NET MVC Testing: Models


Jason Roberts

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:

Part 1 – Automated ASP.NET MVC Testing

Part 2 – Testing the Model

Part 3 – Testing Controllers

Part 4 – Testing View Rendering

Part 5 – Automated Functional UI Testing

Part 6 – Running Tests on TeamCity Continuous Integration Server

Part 7 – Further Learning


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:

  • Is.False
  • Is.True
  • Is.EqualTo
  • Is.Not.EqualTo

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:

  • ShouldPopulateOnCreateLoanApplication

This test saves data to the database, and takes several seconds to run, rather than the milliseconds that the unit tests take to run.

Part 3 Testing Controllers is coming soon

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s