Moq vs NSubstitute - Who is the winner?
Published: 2020-10-16 16:31:32

In this article will compare two of most important mocking libraries for _.Net_ (_.Net Core_ and _.Net Framework_) _**NSubstitute**_ and _**Moq**_.

What will we compare?
- Simplicity
- Readability
- Behaviours

***

The fight begins 💪

# Mock creation

### Moq
```csharp
var mock = new Mock<IRepository>();
```

### NSubstitute
```csharp
var mock = Substitute.For<IRepository>();
```
_**Moq**_ uses a more representative notation. Anyone, just reading that line can know that we are creating a mock object.

> Winner: _**Moq**_


# Mocking properties

### Moq
```csharp
// Simple properties
mock.Setup(foo => foo.Users).Returns(userList);

// Hierarchy
mockUser.Setup(foo => foo.Address.Street).Returns(street);
```

### NSubstitute
```csharp
// Simple properties
mock.Users.Returns(userList);

// Hierarchy
mockUser.Address.Street.Returns(street);
```
_**NSubstitue**_ has a simpler interface to set up the returns.

> Winner: _**NSubstitue**_

# Mocking methods

### Moq
```csharp
// Without parameters
mock.Setup(x => x.ActiveUsers()).Returns(userList);

// Matching by value
mock.Setup(x => x.SearchById(1)).Returns(user);

// Matching by any value of the parameter type
mock.Setup(x => x.SearchById(It.IsAny<int>())).Returns(user);

// Matching by custom logic
mock.Setup(x => x.SearchById(It.Is<int>(i => i < 10))).Returns(user);
```

### NSubstitute
```csharp
// Without parameters
mock.ActiveUsers().Returns(userList);

// Matching by value
mock.SearchById(1).Returns(user);

// Matching by any value of the parameter type
mock.SearchById(Arg.Any<int>()).Returns(user);

// Matching by custom logic
mock.SearchById(Arg.Is<int>(i => i < 10)).Returns(user);
```
Same here, _**NSubstitute**_ has a simpler interface to set up the returns of the methods mocked.

> Winner: _**NSubstitue**_

# Matching arguments

### Moq
```csharp
// - It.IsAny<int>())
// - It.IsInRange(0, 10, Range.Inclusive) 
// - It.IsIn(Enumerable.Range(1, 5))
// - It.IsNotIn(Enumerable.Range(1, 5))
// - It.IsNotNull<string>())
// - It.IsRegex("abc"))
// - It.Is<int>(i => i < 10))
// - mock.Setup(x => x.Search(IsLarge())) //< custom
```

### NSubstitute
```csharp
// - Arg.Any<int>())
// - Arg.Is<int>(i => i < 10))
```
_**Moq**_ provide us a lot more built-in options for matching arguments

> Winner: _**Moq**_

# Capturing parameter

### Moq
```csharp
mock.Setup(x => x.Add(It.IsAny<IUserModel>())).Returns((IUserModel user) => user.Username == "Andres");
```

### NSubstitute
```csharp
mock.Add(Arg.Any<IUserModel>()).Returns(x => { return ((IUserModel)x[0]).Username == "Andres"; });
```

With _**Moq**_, it is easier to get the arguments values before returning the mocked value and process them as needed.

> Winner: _**Moq**_

# Multiple matching

### Moq
```csharp
mock.Setup(x => x.SearchById(It.IsAny<int>())).Returns((int i) => userList.Skip(1).Take(1).First());
mock.Setup(x => x.SearchById(2)).Returns((int i) => userList.First());
```

### NSubstitute
```csharp
mock.SearchById(Arg.Any<int>()).Returns(userList.Skip(1).Take(1).First());
mock.SearchById(2).Returns(userList.First());
```
Both of them allow us to add as many match expressions that we need. If there is a conflict they will take the last expression that matches.

> Winner: _**Tie**_

# Callabacks

### Moq
```csharp
mock.Setup(x => x.SearchById(1))
                .Callback((int x) => parameterValue = x)
                .Returns(userList.First());

// Methods without return
mock.Setup(x => x.Save()).Callback(() => name = "aa");

// before and after
mock.Setup(x => x.SearchById(2))
                .Callback<int>(x => parameterValue = x)
                .Returns(userList.First())
                .Callback<int>(x => name = "andres");
```

### NSubstitute
```csharp
mock.SearchById(1)
                .ReturnsForAnyArgs(x =>
                {
                    parameterValue = (int)x[0];
                    return userList.First();
                });

// Methods without return
mock.When(x => x.Save())
                .Do(x => name = "aa");


// before and after
mock.SearchById(2)
               .ReturnsForAnyArgs(x =>
               {
                   parameterValue = (int)x[0];
                   return userList.First();
               })
               .AndDoes(x => name = "andres");
```
In this case _**Moq***_ provides a more readable experience adding a function called “Callback” on the chain call.

> Winner: _**Moq**_

# Multi-returns

### Moq
```csharp
mock.SetupSequence(x => x.Users)
                .Returns(users1)
                .Returns(users2)
                .Returns(users3);
```

### NSubstitute
```csharp
mock.Search(Arg.Any<string>()).Returns(users1, users2, users3);
```
Here both libraries are very similar right?.

> Winner: _**Tie**_

# Throwing Exceptions

### Moq
```csharp
mock.Setup(x => x.Save()).Throws<Exception>();
mock.Setup(x => x.Save()).Throws(new Exception("msj"));
```

### NSubstitute
```csharp
mock.ActiveUsers().Returns(x => { throw new Exception(); });
mock.When(x => x.Save())
                .Do(x => { throw new Exception("msj"); });
```
_**Moq**_ is easier to recognize that the function will throw an exception.

> Winner: _**Moq**_

# Verify

### Moq
```csharp
// Setter
mock.VerifySet(x => x.Users = users);

// Getter
mock.VerifyGet(x => x.Users);

// Methods with matchting
mock.Verify(x => x.Search(It.IsAny<string>()));
mock.Verify(x => x.Search("aaa"), Times.Never());

// Occurrences
// - Times.Never
// - Times.Once
// - Times.AtLeastOnce
// - Times.AtMost(2)
// - Times.Exactly(2)
```

### NSubstitute
```csharp
// Setter
mock.Received().Users;

// Getter
mock.Received().Users = users;

// Methods with matchting
mock.Received().Search("aa");
mock.DidNotReceive().Search("aaa");
mock.Received(2).Search("bb");

// Occurrences
// Nop

```
_**NSubsitute**_ has a simple way to verify that a method was called but Moq has more options to test how many times the method was executed.

> Winner: _**Moq**_


# Matching Generic Type Arguments

### Moq
```csharp
mock.Setup(m => m.AddUser(It.IsAny<It.IsSubtype<IUserModel>>())).Returns(true);
mock.Setup(m => m.AddUser(It.IsAny<UserModel2>())).Returns(false);
```

### NSubstitute
```csharp
mock.AddUser<IUserModel>(Arg.Any<IUserModel>()).Returns(true);
mock.AddUser<IUserModel>(Arg.Any<UserModel2>()).Returns(false);
```
Again, both provide an easy way to mock Generic Methods

> Winner: _**tie**_


***

Well, how is it going? Who is winning? 
Let’s see the current score


| Syntax      | Moq | NSubstitute |
| ----------- | ----------- | ----------- |
| Mock creation | ✅ |  |
| Mocking properties | | ✅ |
| Mocking methods   | | ✅ |
| Matching arguments   | ✅ |  |
| Multiple matching   | tie | tie |
| Callabacks   | ✅ |  |
| Multi-returns   | tie | tie |
| Throwing Exceptions   | ✅ |  |
| Verify   | ✅ |  |
| Matching Generic Type Arguments   | tie | tie  |


_**Moq**_ is the winner!! 😀 🎉

I really prefer _**Moq**_ and I try to use it always but both are excellent options to use. Us, as developers, we should be able to work with both.

You can download the examples from my Github
https://github.com/andreslozadamosto/net-thoughts/tree/master/libraries/testing/MoqVsNSubstitute/MockingLibrariesExamples 

*** 

Are you with me? 
Do you prefer _**NSubstitue**_? 
I forget to compare any behaviour?

Share your comments 🤙👇