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
85
votes
8 answers

Moq IServiceProvider / IServiceScope

I am trying to create a Mock (using Moq) for an IServiceProvider so that I can test my repository class: public class ApiResourceRepository : IApiResourceRepository { private readonly IServiceProvider _serviceProvider; public…
blgrnboy
  • 4,877
  • 10
  • 43
  • 94
85
votes
2 answers

MOQ - setting up a method based on argument values (multiple arguments)

I have an interface defined as interface IMath { AddNumbersBetween(int lowerVal, int upperVal); } I can setup a basic Moq for the above as follows: Mock mock = new Mock(); mock.Setup(m => m.AddNumbersBetween(It.IsAny(),…
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
85
votes
2 answers

Moq ReturnsAsync() with parameters

I'm trying to mock a repository's method like that public async Task GetByTypeValue(WhitelistType type, string value) using Moq ReturnsAsync, like this: static List whitelist = new List(); var…
Daniel Medina
  • 887
  • 1
  • 6
  • 8
82
votes
3 answers

How to set up a method twice for different parameters with Moq

I would like to set up a method with Moq twice but it seems that the last one overrides the previous ones. Here's my initial setup: string username = "foo"; string password = "bar"; var principal = new GenericPrincipal( new…
tugberk
  • 57,477
  • 67
  • 243
  • 335
80
votes
10 answers

Using Moq to verify calls are made in the correct order

I need to test the following method: CreateOutput(IWriter writer) { writer.Write(type); writer.Write(id); writer.Write(sender); // many more Write()s... } I've created a Moq'd IWriter and I want to ensure that the Write() methods…
g t
  • 7,287
  • 7
  • 50
  • 85
78
votes
4 answers

Moq + Unit Testing - System.Reflection.TargetParameterCountException: Parameter count mismatch

I'm tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, null); line. System.Reflection.TargetParameterCountException: Parameter count…
myermian
  • 31,823
  • 24
  • 123
  • 215
78
votes
3 answers

How to assign values to properties in moq?

I have a class with a method that returns an object of type User public class CustomMembershipProvider : MembershipProvider { public virtual User GetUser(string username, string password, string email, bool isApproved) { return new…
ridermansb
  • 10,779
  • 24
  • 115
  • 226
77
votes
2 answers

What is the purpose of VerifyAll() in Moq?

I read the question at What is the purpose of Verifiable() in Moq? and have this question in my mind: What is the purpose of VerifyAll() in Moq?
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
77
votes
2 answers

How to add an item to a Mock DbSet (using Moq)

I'm trying to set up a mock DbSet for testing purposes. I used the tutorial here, http://www.loganfranken.com/blog/517/mocking-dbset-queries-in-ef6/ and slightly modified it so calling GetEnumerator returns a new enumerator each time (another…
user1080952
  • 1,073
  • 2
  • 10
  • 15
76
votes
2 answers

Why use It.is<> or It.IsAny<> if I could just define a variable?

Hi I've been using moq for a while when I see this code. I have to setup a return in one of my repo. mockIRole.Setup(r => r.GetSomething(It.IsAny(), It.IsAny(), It.IsAny())).Returns(ReturnSomething); I have…
choopau
  • 2,209
  • 5
  • 21
  • 28
76
votes
3 answers

Settings variable values in a Moq Callback() call

I think I may be a bit confused on the syntax of the Moq Callback methods. When I try to do something like this: IFilter filter = new Filter(); List objects = new List { new Foo(), new Foo() }; IQueryable myFilteredFoos =…
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
74
votes
2 answers

Using Verify to confirm expected parameter values in Moq mock class

I'm trying to verify that a method within a mock is called with an expected object parameter. I'm using Moq, nUnit, and thinking that AutoFixture's Likeness should get the job done. Below is a simplified version of what i'm trying to do. Is there…
jaminto
  • 3,895
  • 3
  • 32
  • 36
73
votes
7 answers

Is it possible to mock out a .NET HttpWebResponse?

i've got an integration test that grabs some json result from a 3rd party server. It's really simple and works great. I was hoping to stop actually hitting this server and using Moq (or any Mocking library, like ninject, etc) to hijack and force the…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
72
votes
1 answer

SetupSet() is obsolete. In place of what?

Let's say I want to use Moq to create a callback on a setter to store the set property in my own field for later use. (Contrived example - but it gets to the point of the question.) I could do something like this: myMock.SetupSet(x =>…
RationalGeek
  • 9,425
  • 11
  • 62
  • 90
71
votes
3 answers

Moq'ing methods where Expression> are passed in as parameters

I'm very new to unit testing and mocking! I'm trying to write some unit tests that covers some code that interacts with a data store. Data access is encapsulated by IRepository: interface IRepository { .... IEnumerable
jonsidnell
  • 1,210
  • 1
  • 10
  • 20