Mocking in .NET Core Unit Tests with Moq: Getting Started

Moq is the most popular mocking framework for .NET. This course will teach you how to isolate parts of your codebase for easier testing by creating and configuring test-time-only mock versions of dependencies.
Course info
Rating
(55)
Level
Intermediate
Updated
Dec 1, 2017
Duration
1h 57m
Table of contents
Course Overview
Getting Started with Mocking and Moq
Mocking Method Calls
Mocking Properties
Implementing Behavior Verification Tests
Using Additional Mocking Techniques
Description
Course info
Rating
(55)
Level
Intermediate
Updated
Dec 1, 2017
Duration
1h 57m
Description

Writing unit tests can be difficult, time-consuming, and slow when you can't isolate the classes you want to test from the rest of the system. In this course, Mocking in .NET Core Unit Tests with Moq: Getting Started, you'll learn how to create mocks and use them as dependencies to the classes you want to test. First, you'll discover how to configure mocked methods and properties to return specific values. Next, you'll cover how to perform behavior/interaction testing. Finally, you'll explore how to setup mocked exceptions and events. When you're finished with this course, you'll have the necessary knowledge to use Moq to unit test your classes in isolation by creating and using mock objects.

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
12 Oct 2018
Error Handling in C# with Exceptions
Intermediate
1h 37m
6 Aug 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, Mocking in. NET Core Unit Tests with Moq: Getting Started. I'm a Microsoft MVP, freelance developer and author. In this course we're going to learn how to get started writing unit tests that can isolate only those parts of the application you actually want to test by creating and using mock objects. Some of the major topics that we'll cover on this course include how to create mocks and use them as dependencies to the classes you're testing, how to configure the return values of mock methods and properties, how to verify that your classes are interacting correctly with mocks, and also how to mock exceptions and events. By the end of this course you'll understand how mocks help to isolate parts of the code base to make testing subsets of the application easier, how to create and configure Moq mock objects, and how to use them as test time dependencies for the classes you're testing. Before beginning the course it's recommended that you have at least a basic understanding of C#. I hope you'll join me on this journey with the Mocking in. NET Core Unit Tests with Moq: Getting Started course at Pluralsight.

Getting Started with Mocking and Moq
Hi, I'm Jason Roberts from Pluralsight. Welcome to this course, Mocking in. NET Core Unit Tests with Moq: Getting Started. Automated unit tests provide a whole host of benefits when we're writing software to check that the system is working as expected, and as we add functionality, that we haven't broken anything in the process. Sometimes, however, it can be quite painful and frustrating to write unit tests when the classes we're testing require dependencies that are difficult to use all set up. In this course we're going to learn about the Moq mocking framework and how it can allow us to write tests more easily by creating test time only mock versions of dependencies. In this first module, we're going to start off by getting an overview of mocking itself and how it allows us to isolate parts of the code base. We'll then learn some of the reasons why we might want to use mocking in the first place and we'll briefly define what a unit is when we're talking about unit tests. We'll learn about some of the different terminology out there, such as fakes, stubs, mocks, and test doubles, and then we'll jump into Visual Studio and have a look at the demo code we'll be using in this course. We'll then go and add a new unit test project to our solution and we'll go and write some initial tests. Once these tests are passing, we'll introduce a new dependency to the class we're testing and we'll see how this breaks the existing tests. We'll see that this new dependency that we've introduced is hard to use. Finally, we'll install Moq into our test project, ready to create our first mock objects.

Mocking Method Calls
Hi, welcome back. We finished off the previous module by installing Moq into our testing project. In this module we're going to create our first mock objects and we're going to learn how we can set up the mock object methods. So we're going to start off by instantiating and using a Moq mock object. Recall from the previous module that we've got two tests where we're having to pass nulls to the CreditCardApplicationEvaluator constructor. This is causing a NullReferenceException and breaking one of the tests. To get around having to pass null values to the constructor, we're going to instead be using mock objects and we're going to configure the mock object methods to return specific values, such as returning a value regardless of the parameters passed to the mock method, returning a value only when a specified predicate is satisfied, specifying ranges of argument values, and also using regular expressions. We'll also learn about Moq's strict and loose mocking behavior and we'll wrap up this module by seeing how we can deal with mocked methods that have out parameters. So let's head over to Visual Studio now and create our first mock object.

Mocking Properties
Hi, welcome back. In the previous module we learned how to configure the methods of our mock objects. Dependencies, of course, don't just have methods, they can also have properties. So in this module we're going to learn how we can configure the properties of our mock objects. We'll start off by configuring a mocked property to return a specific value and in addition to hard coding a literal value, we can also get the value from a function. We'll learn how Moq can help us out if we've got a lot of nested properties that we want to mock and also how we can change the default behavior that Moq uses when assigning default property values. Finally, we'll learn how we can set up the properties on our mock objects to actually track changes. So this means if we change the value of a mock property, it will actually remember this value for later use. So let's get started by learning how we can configure a mocked property to return a specific value.

Implementing Behavior Verification Tests
Hi, welcome back. In the previous module we learned how to set up the properties of our mock objects. In this module we're going to learn how we can use Moq to implement behavior verification tests. In this module we're going to start off with an overview of behavior based testing versus state based testing. We'll then jump into Visual Studio and we'll learn how Moq can help us to verify that a method was called and if the verification fails, how we can add a custom error message to give us more test failure context. In addition to verifying that a method was called, we'll also learn how we can verify that a method wasn't called, and also how to verify that a method was called a specific number of times. We'll learn how to use Moq to verify that a mocked property getter was called and, similarly, how to verify that a property setter was called. So let's kick off this module by getting an overview of the difference between behavior verification testing and state based testing.

Using Additional Mocking Techniques
Hi, welcome back to this final module. In the previous module we learned how we can use Moq to perform behavior based or interaction based testing. In this module we're going to learn about some additional mocking techniques that we can make use of when we're using Moq. In this module we're going to learn how we can set up our mock objects to throw exceptions, how we can raise events from mock objects, and we'll learn how we can do this manually or we can set up our mock object to raise events automatically when a method is executed. We'll learn that we can set up different return values for sequential calls on a single mock object, and we'll learn that instead of creating a mock object from an interface, we can create mock versions of concrete types. And in addition to mocking concrete types, we'll also learn a technique that allows us to mock protected members of the concrete type. We'll learn an alternative syntax to setting up mock objects and this may help to improve mock setup code readability. To do this we make use of Moq's LINQ to mocks syntax. And finally, we'll go and refactor the test class and tidy things up. So let's head over to Visual Studio and learn how we can set up mock objects to throw exceptions.