---
title: "What are mocking frameworks you’ve used with .NET? Examples?"  
description: "What are mocking frameworks you’ve used with .NET? Examples?"  
author: "Ravi Vishwakarma"  
published: 2025-06-17  
updated: 2025-06-17  
canonical: https://www.mindstick.com/interview/34252/what-are-mocking-frameworks-you-ve-used-with-dot-net-examples  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What are mocking frameworks you’ve used with .NET? Examples?

Mocking frameworks are essential in .NET unit testing for isolating dependencies and simulating behavior of external components like services, databases, or APIs. Here are some of the **most popular mocking frameworks** used with .NET, along with **examples**:

## 1. Moq (Most Popular)

**Moq** is a widely used and easy-to-learn mocking library for .NET.

### Installation

```plaintext
dotnet add package Moq
```

### Example – Mocking a service

#### Interface:

```cs
public interface IUserService
{
    string GetUserName(int id);
}
```

#### Test using Moq:

```cs
var mock = new Mock<IUserService>();
mock.Setup(s => s.GetUserName(1)).Returns("Anna");

var controller = new UsersController(mock.Object);
var result = controller.GetUserName(1); // Should return "Anna"

Assert.Equal("Anna", result);
```

## 2. NSubstitute

A simpler and more intuitive alternative to Moq.

### Installation

```cs
dotnet add package NSubstitute
```

### Example:

```cs
var userService = Substitute.For<IUserService>();
userService.GetUserName(1).Returns("Anna");

var result = userService.GetUserName(1);
Assert.Equal("Anna", result);
```

## 3. FakeItEasy

Very readable syntax; good for newcomers.

### Installation

```cs
dotnet add package FakeItEasy
```

### Example:

```cs
var service = A.Fake<IUserService>();
A.CallTo(() => service.GetUserName(1)).Returns("Anna");

var result = service.GetUserName(1);
Assert.Equal("Anna", result);
```

## 4. JustMock (Telerik)

- Supports mocking **non-virtual** members, **static**, and **sealed classes**.
- Has both free and paid editions.

### Example:

```cs
var service = Mock.Create<IUserService>();
Mock.Arrange(() => service.GetUserName(1)).Returns("Anna");

Assert.Equal("Anna", service.GetUserName(1));
```

## 5. Microsoft Fakes (only in Visual Studio Enterprise)

- Can mock **static** and **sealed** classes.
- Available in **VS Enterprise** only.
- Uses **shim** and **stub** concepts.

## Summary Table

| Framework | Syntax Style | Can Mock Sealed/Static | Popularity | Notes |
| --- | --- | --- | --- | --- |
| Moq | Fluent | (only interfaces/virtual) | ⭐⭐⭐⭐⭐ | Most common, widely used |
| NSubstitute | Natural | Yes | ⭐⭐⭐⭐ | Simple and clean syntax |
| FakeItEasy | Natural | Yes | ⭐⭐⭐⭐ | Very readable |
| JustMock | Fluent | (paid version) | ⭐⭐⭐ | Paid version powerful |
| Microsoft Fakes | Stub/Shim | Yes | ⭐⭐ | Only in VS Enterprise |

## Answers

### Answer by Ravi Vishwakarma

Mocking frameworks are essential in .NET unit testing for isolating dependencies and simulating behavior of external components like services, databases, or APIs. Here are some of the **most popular mocking frameworks** used with .NET, along with **examples**:

## 1. Moq (Most Popular)

**Moq** is a widely used and easy-to-learn mocking library for .NET.

### Installation

```plaintext
dotnet add package Moq
```

### Example – Mocking a service

#### Interface:

```cs
public interface IUserService
{
    string GetUserName(int id);
}
```

#### Test using Moq:

```cs
var mock = new Mock<IUserService>();
mock.Setup(s => s.GetUserName(1)).Returns("Anna");

var controller = new UsersController(mock.Object);
var result = controller.GetUserName(1); // Should return "Anna"

Assert.Equal("Anna", result);
```

## 2. NSubstitute

A simpler and more intuitive alternative to Moq.

### Installation

```cs
dotnet add package NSubstitute
```

### Example:

```cs
var userService = Substitute.For<IUserService>();
userService.GetUserName(1).Returns("Anna");

var result = userService.GetUserName(1);
Assert.Equal("Anna", result);
```

## 3. FakeItEasy

Very readable syntax; good for newcomers.

### Installation

```cs
dotnet add package FakeItEasy
```

### Example:

```cs
var service = A.Fake<IUserService>();
A.CallTo(() => service.GetUserName(1)).Returns("Anna");

var result = service.GetUserName(1);
Assert.Equal("Anna", result);
```

## 4. JustMock (Telerik)

- Supports mocking **non-virtual** members, **static**, and **sealed classes**.
- Has both free and paid editions.

### Example:

```cs
var service = Mock.Create<IUserService>();
Mock.Arrange(() => service.GetUserName(1)).Returns("Anna");

Assert.Equal("Anna", service.GetUserName(1));
```

## 5. Microsoft Fakes (only in Visual Studio Enterprise)

- Can mock **static** and **sealed** classes.
- Available in **VS Enterprise** only.
- Uses **shim** and **stub** concepts.

## Summary Table

| Framework | Syntax Style | Can Mock Sealed/Static | Popularity | Notes |
| --- | --- | --- | --- | --- |
| Moq | Fluent | (only interfaces/virtual) | ⭐⭐⭐⭐⭐ | Most common, widely used |
| NSubstitute | Natural | Yes | ⭐⭐⭐⭐ | Simple and clean syntax |
| FakeItEasy | Natural | Yes | ⭐⭐⭐⭐ | Very readable |
| JustMock | Fluent | (paid version) | ⭐⭐⭐ | Paid version powerful |
| Microsoft Fakes | Stub/Shim | Yes | ⭐⭐ | Only in VS Enterprise |


---

Original Source: https://www.mindstick.com/interview/34252/what-are-mocking-frameworks-you-ve-used-with-dot-net-examples

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
