ASP.NET Core MVC Testing Fundamentals

ASP.NET Core MVC is the latest evolution of .NET web programming. In this course, you'll learn how to create automated tests to cover the different parts of your ASP.NET Core MVC application to help find errors before they make it to production.
Course info
Rating
(58)
Level
Intermediate
Updated
Jun 19, 2017
Duration
2h 56m
Table of contents
Course Overview
Introduction
Testing Model Classes with xUnit.NET
Creating and Using Mock Dependencies
Writing MVC Controller Tests
Writing ASP.NET Core MVC Integration Tests
Implementing Automated Functional UI Testing with Selenium
Description
Course info
Rating
(58)
Level
Intermediate
Updated
Jun 19, 2017
Duration
2h 56m
Description

No one likes the stress of emergencies caused by errors in code that have been released to production, especially if these errors happen early in the morning and require emergency fixes. Automated tests help check the application is working as expected, and reduces the likelihood of errors making it to the production environment. Fewer production errors mean happier end users, development teams, and business owners. In this course, ASP.NET Core MVC Testing Fundamentals, you'll learn how to create automated tests for your ASP.NET Core MVC application, including model, controller, and automated UI tests. First, you'll dive into getting started with xUnit.net, and how to specifically test your model. Next, you'll explore how to isolate parts of your code base by using mock objects and the Moq library for .NET Core. Finally, you'll cover how to automate the browser to create functional UI tests. By the end of this course, you'll have the knowledge to create automated unit, integration, and UI tests for your ASP.NET Core MVC application. Additionally, you'll know how to execute these tests to verify the correctness of your application, and help ensure errors do not make it to production.

About the author
About the author

With over 15 years experience, Jason Roberts is a Microsoft .NET MVP, freelance developer, and author.

More from the author
Working with Files and Streams in C#
Intermediate
3h 13m
Oct 12, 2018
Error Handling in C# with Exceptions
Intermediate
1h 37m
Aug 6, 2018
More courses by Jason Roberts
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone, my name is Jason Roberts. Welcome to my course ASP. NET Core MVC Testing Fundamentals. I am a Microsoft. NET MVP, freelance developer and author. In this course we're going to learn how to write automated tests at different levels for your ASP. NET Core application, including tests for models and controllers, integration tests, and automated UI tests. Some of the major topics that we'll cover in this course include how to get started with xUnit. net, how to test your model and controllers, how to isolate classes being tested with mock objects, how to perform in-memory integration tests that also use in-memory Entity Framework Core databases, and how to automate the browser to create functional UI tests. By the end of this course you'll understand how to ensure your ASP. NET Core application is working as expected by creating a suite of automated tests at different levels. Before beginning the course you should be familiar with writing basic C# code and have a basic understanding of ASP. NET MVC. I hope you'll join me on this journey with the ASP. NET Core MVC Testing Fundamentals course at Pluralsight.

Testing Model Classes with xUnit.NET
Hi, welcome back. In the previous module we learned why we might want to write automated tests and some of the benefits they provide. We also got a brief look at the testing pyramid and the different types of tests such as unit, integration, subcutaneous, and functional UI. In this module we're going to start writing some tests and we're going to start testing the Model classes in isolation. Later in the course we'll see how we can write both integration tests and also isolate parts of the code base by using mock objects. So in this module we're going to start off by getting an overview of the xUnit. net testing framework that we'll be using throughout this course. As part of this overview we'll learn about the different test attributes that xUnit. net provides, how we can check that our code is doing the correct thing by using assertions, and also the different ways that we can execute our xUnit. net tests. We're then going to go and add some new functionality to our CreditCardsApplication that we saw in the previous module. We're going to go and create some new model classes and then we're going to go and add a new xUnit. net test project to our solution. In this new test project we're going to write some unit tests against the model classes that we'll be creating. We're going to create a new FrequentFlyeNumberValidator class and also a new CreditCardApplicationEvaluator class. Later on in the course we'll be making use of these classes to add new functionality to the application and the user interface. So let's kick off this module by getting an overview of xUnit. net.

Creating and Using Mock Dependencies
Hi, welcome back. In the previous module we learned how we can test our model classes using the xUnit. net testing framework. In this module we're going to be learning how we can create and use mock dependencies to isolate parts of the application that we want to test. So in this module we're going to start off by learning how mocks help us isolate parts of the application. This allows us to test only those parts of the applications that we want to at any given time. Mock objects help us to test in isolation, so we're going to take a moment to discuss why we actually want to test in isolation in the first place. We're then going to learn about a mocking library called Moq that allows us to create mocks in our. NET Core tests. We'll then jump into Visual Studio and we'll start to refactor our CreditCardApplicationEvaluator that we created in the previous module. We're going to change it to take a dependency on the FrequentFlyerNumberValidator that we created. Once we've done this, we can add some additional business logic to the CreditCardApplicationEvaluator to take a count of the frequent flyer number that was passed in. Once we've modified our model classes, we'll go and modify the tests that we created in the previous module. As part of the refactoring of our tests, we'll see how we can create a mock FrequentFlyerNumberValidator using the moq library. Doing this will allow us to test the CreditCardApplicationEvaluator in isolation. So let's kick off this module by seeing how we can isolate parts of the application we're testing using mock objects.

Writing MVC Controller Tests
Hi, welcome back. In the previous module we learned how to use Moq to isolate the parts of the application that we're testing. So we learned how to isolate parts of our model when we're writing our tests. In some ways controllers are the heart of our MVC applications, they'll like the central glue that holds the application together. Because of the importance of controllers, it's often beneficial to have some test coverage of them. So in this module we're going to start off by getting an overview of controller testing and we'll learn the types of tests that we can write against our controllers. We'll then jump into Visual Studio and we'll go and create our first controller test, once again using xUnit. net as our testing framework. We'll then go and write some additional tests, for example, testing that the controller action does the right thing when we have an invalid model state. We'll learn how we can use Moq to create mock dependencies that our controllers need and we'll see how we can assert that the corrects views are returned from controller actions. We'll also learn how we can go and write tests for our API value controllers. So let's kick off this module by getting an overview of the kinds of tests we can write against our MVC controllers.

Implementing Automated Functional UI Testing with Selenium
Hi, welcome back. In the previous module we learned how to create integration tests for our ASP. NET Core MVC application. In this final module we're going to be rounding out the course and learning how we can implement automated functional user interface tests using the Selenium open source library. So in this module we're going to start off by getting a high level overview of Selenium and how it fits together with our test code. We'll then jump into Visual Studio and we'll create a functional UI test project and install the Selenium NuGet packages. We'll then write an initial Selenium test that opens up Chrome, navigates to the CreditCardApply page and makes a basic assert that the page was loaded correctly. Once we've got this initial test working, we'll go and write a more complex test, this time filling out the application form for the credit card and checking that if we have incorrect data then we get validation errors. Next we'll go and test the end to end application flow for a new credit card. So we're going to go and create a new credit card application in the browser, we'll click the Apply button and we'll check that we're taken to the correct screen with the correct expected decision. At this point in the demos we'll see that we've got a lot of code duplication creeping in and the maintainability of our tests is decreasing. To fix this we're going to implement something called the Page Object Model pattern, which allows us to create models for individual pages in the application and then reuse those models throughout the test code. So let's kick off this module by getting a high level overview of Selenium.