C# Unit Tests with Mocks
Tags: dotnet, unittest, qa, csharp
Published: 2020-01-22 15:38:25

---
title: C# Unit Tests with Mocks
published: true
description: C# Unit Tests with Mocks provide an easy way of reducing unwanted dependencies when writing unit tests.
tags: dotnet, unit test, qa, csharp
---

This post was originally published [here](https://www.patrickschadler.com/c-unit-tests-mocks/).

C# Unit Tests with Mocks provide an easy way of reducing unwanted dependencies when writing unit tests. One of the best and shortest definition for mocking is *mocking is creating objects that simulate the behavior of real objects. - [StackOverflow](https://stackoverflow.com/questions/2665812/what-is-mocking)*.

## Prologue

However, when talking about writing unit tests I would strongly advise to not change the implementation itself. Rather, think about how you can test it easily before start writing the implementation. The very best approach for unit-testing would be [Test-Driven-Development](http://agiledata.org/essays/tdd.html) (TDD) anyway, but I understand that many struggles with this methodology. Especially when starting with tests in general. Always Keep in mind that the [S.O.L.I.D](http://www.patrickschadler.com/solid-principles-part-i/) Framework helps you a lot when writing unit tests. 

## Moq Framework

Moq is an open-source framework which can be found on [GitHub](https://github.com/moq/moq4). When start using Moq make sure you always install the very latest version. Moq is usable with any test runner like [vstest](https://github.com/Microsoft/vstest), [xunit](https://xunit.github.io/), [nunit](https://nunit.org/) etc. When using Moq or any other mocking framework keep following restrictions in mind. You can not mock: 

1. Static class/methods.
2. Extensions Methods.
3. Non-virtual methods (except there is an interface).

For this blog post, I have used my [link-reader repository](https://github.com/DonkeyKongJr/link-reader). This project is written with dotnet core 2.1. 

{% gist https://gist.github.com/DonkeyKongJr/894c5b3a5aaea81f04e61fa3516d224c %}

However, you can easily use a newer version of dotnet core if you want. You can see that in this particular case it is necessary to nest multiple mocks within each other. You can configure that one mock mocks a method and return another mock object. To generate a mock you just have to create a new Mock object and set the type. As you see Moq uses generics very heavily. 
```
var moq = new Mock<IWebRequest>();
```

When you look at the IWebRequest definition you'll see that the interface has a Create method that all classes have to implement. Therefore, we can easily mock this method in our case by using the Setup. 
```
moq.Setup(_ => _.Create(It.IsAny<string>())).Returns(moqHttpWebRequest.Object);
```

When you want to mock a method with parameters you can work with the *It.IsAny<type>()* option. In the example above all string values are allowed. Of course, you can strict this up and allow only specific values to work with. To work with mocks you have to use the *mock*.Object property. This property is the actual mock implementation of the source. 
```
var webRequest = moq.Object.Create(test);
```

The line above returns the *moqHttpWebRequest.Object*, because that's the way we have set up the mock behavior. 

## Customizing Mock Behavior

Moq provides different ways to set up the mock - [MockBehavior.Strict](https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior)
- [MockBehavior.Loose](https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior)
- [CallBase = true](https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior)
- [{ DefaultValue = DefaultValue.Mock }](https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior)
- [MockBehavior.Strict and { DefaultValue = DefaultValue.Mock }](https://github.com/Moq/moq4/wiki/Quickstart#customizing-mock-behavior)

## Verification

When you are using the [verification](https://github.com/Moq/moq4/wiki/Quickstart#verification) from a mock instance by calling *moq.Verify()* it does check if all set up methods are called. You can check all Moq Setups without any Verifiable declaration by using the *moq.VerifyAll()* method on the mocked object. Happy mocking and testing.