Questions tagged [moq]

Moq is a strongly typed and minimalistic mocking framework for .NET.

Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET Linq expression trees and lambda expressions, which makes it the most productive, type-safe and refactoring-friendly mocking library available. It supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn't require any prior knowledge or experience with mocking concepts.


Resources

Project code

Wiki

Quick start

Installing Moq can most easily be done using its NuGet package:

Install-Package Moq
5720 questions
106
votes
10 answers

How to mock an async repository with Entity Framework Core

I'm trying to create a unit test for a class that calls into an async repository. I'm using ASP.NET Core and Entity Framework Core. My generic repository looks like this. public class EntityRepository : IEntityRepository where…
Jed Veatch
  • 2,326
  • 3
  • 12
  • 13
104
votes
6 answers

How do I use Moq to mock an extension method?

I am writing a test that depends on the results of an extension method but I don't want a future failure of that extension method to ever break this test. Mocking that result seemed the obvious choice but Moq doesn't seem to offer a way to override…
patridge
  • 26,385
  • 18
  • 89
  • 135
100
votes
6 answers

Mocking EF DbContext with Moq

I'm trying to create a unit test for my service with a mocked DbContext. I created an interface IDbContext with the following functions: public interface IDbContext : IDisposable { IDbSet Set() where T : class; DbEntityEntry
Gaui
  • 8,723
  • 16
  • 64
  • 91
99
votes
7 answers

moq objects Returns method, should return a null object

I'm developing a Web API, and one of the test I came up with is that, if client makes a GET operation with a Physical Test ID (Physical Test is the resource I'm looking for) and that physical test is not found, the web API should return a 404…
Daniel
  • 2,484
  • 4
  • 27
  • 35
98
votes
3 answers

How to mock ModelState.IsValid using the Moq framework?

I'm checking ModelState.IsValid in my controller action method that creates an Employee like this: [HttpPost] public virtual ActionResult Create(EmployeeForm employeeForm) { if (this.ModelState.IsValid) { IEmployee employee =…
Mazen
  • 1,487
  • 2
  • 13
  • 14
98
votes
7 answers

SetupSequence in Moq

I want a mock that returns 0 the first time, then returns 1 anytime the method is called thereafter. The problem is that if the method is called 4 times, I have to write: mock.SetupSequence(x => x.GetNumber()) .Returns(0) .Returns(1) …
Florian
  • 4,507
  • 10
  • 53
  • 73
97
votes
7 answers

Mocking generic methods in Moq without specifying T

I have an interface with a method as follows: public interface IRepo { IA Reserve(); } I would like to mock the class that contains this method without having to specify Setup methods for every type it could be used for. Ideally, I'd…
Wilbert
  • 7,251
  • 6
  • 51
  • 91
95
votes
4 answers

Multiple Moq It.Is() Matching Arguments

With Moq, is it valid to have more than one Matching Argument? It.Is() In this example I want the mockMembershipService to return a different ProviderUserKey depending on the User supplied. mockMembershipService.Setup( x => x.GetUser( …
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
92
votes
7 answers

How to mock static methods in c# using MOQ framework?

I have been doing unit testing recently and I've successfully mocked various scenarios using MOQ framework and MS Test. I know we can't test private methods but I want to know if we can mock static methods using MOQ.
Himanshu
  • 2,251
  • 4
  • 20
  • 25
90
votes
3 answers

Mocking a method to throw an exception (moq), but otherwise act like the mocked object?

I have a Transfer class, simplified it looks like this: public class Transfer { public virtual IFileConnection source { get; set; } public virtual IFileConnection destination { get; set; } public virtual void GetFile(IFileConnection…
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
89
votes
8 answers

Mocking Static Methods

Recently, I've begun to use Moq to unit test. I use Moq to mock out classes that I don't need to test. How do you typically deal with static methods? public void foo(string filePath) { File f = StaticClass.GetFile(filePath); } How could this…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
88
votes
6 answers

Mocking EF core dbcontext and dbset

I am using ASP.NET Core 2.2, EF Core and MOQ. When I run the test I am getting this error: Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.Movies What I am doing wrong? public class…
MarcosF8
  • 1,848
  • 5
  • 19
  • 33
88
votes
8 answers

Reset mock verification in Moq?

Setup as so: public interface IFoo { void Fizz(); } [Test] public void A() { var foo = new Mock(MockBehavior.Loose); foo.Object.Fizz(); foo.Verify(x => x.Fizz()); // stuff here foo.Verify(x => x.Fizz(),…
fostandy
  • 4,282
  • 4
  • 37
  • 41
87
votes
1 answer

MOQ - how to mock an interface that needs to be cast to another interface?

what I want to do is construct a moq for I1 - which is fine ... however in the course of the method that I am testing that uses this mock I need to cast it to I2 in order to access some properties that are not on I1 Interface I1 { int…
John Nicholas
  • 4,778
  • 4
  • 31
  • 50
87
votes
2 answers

Mock an update method returning a void with Moq

In my test, I defined as data a List with some record in. I'd like setup a moq the methode Update, this method receive the user id and the string to update. Then I get the the IUser and update the property LastName I tried this : namespace…
TheBoubou
  • 19,487
  • 54
  • 148
  • 236