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
70
votes
3 answers

Passing Moq mock-objects to constructor

I've been using RhinoMocks for a good while, but just started looking into Moq. I have this very basic problem, and it surprises me that this doesn't fly right out of the box. Assume I have the following class definition: public class Foo { …
stiank81
  • 25,418
  • 43
  • 131
  • 202
70
votes
7 answers

Mocking Asp.net-mvc Controller Context

So the controller context depends on some asp.net internals. What are some ways to cleanly mock these up for unit tests? Seems like its very easy to clog up tests with tons of setup when I only need, for example, Request.HttpMethod to return…
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
70
votes
4 answers

Where is the MOQ documentation?

Where can I find comprehensive documentation for MOQ? I'm just starting with mocking and am having difficulty getting my head around it. I've read through all the links at http://code.google.com/p/moq/wiki/QuickStart but can't seem to find a…
Jeremy Holt
  • 987
  • 1
  • 9
  • 14
69
votes
3 answers

Mock IHttpContextAccessor in Unit Tests

I have a method to get header value using IHttpContextAccessor public class HeaderConfiguration : IHeaderConfiguration { public HeaderConfiguration() { } public string GetTenantId(IHttpContextAccessor httpContextAccessor) { …
superninja
  • 3,114
  • 7
  • 30
  • 63
69
votes
4 answers

Moq, strict vs loose usage

In the past, I have only used Rhino Mocks, with the typical strict mock. I am now working with Moq on a project and I am wondering about the proper usage. Let's assume that I have an object Foo with method Bar which calls a Bizz method on object…
Bryan Rowe
  • 9,185
  • 4
  • 26
  • 34
69
votes
7 answers

Mock.Of VS Mock()
I'm currently confuse on how to mock. I'm using Moq. To mock objects I usually write this way var mockIRepo = new Mock(); However, I need to create mock object for my setup. Option1 Is it better to mock my object which only contain…
choopau
  • 2,209
  • 5
  • 21
  • 28
67
votes
3 answers

Setup Method With Params Array

I am developing tests for an application. There's a method that has a params array as a parameter. I have set up the method using Moq but when I run the test, the return value of the method is null, which means it is not being mocked. Here's a code…
Luis Aguilar
  • 4,331
  • 6
  • 36
  • 55
67
votes
2 answers

Setup() vs SetupGet()

What is the difference between the SetupGet() and Setup() methods for MOQ?
Nil Pun
  • 17,035
  • 39
  • 172
  • 294
67
votes
4 answers

How to mock a class that implements multiple interfaces

How to mock the following class: UserRepository : GenericRepository, IUserRepository public class GenericRepository : IGenericRepository where TEntity : class I am using Moq, and I am confused how to handle multiple…
loyalflow
  • 14,275
  • 27
  • 107
  • 168
65
votes
2 answers

Calling original method with Moq

I have a ProductRepository with 2 methods, GetAllProducts and GetProductByType, and I want to test the logic at GetProductByType. Internally, GetProductByType makes a call to GetAllProducts and then filters the correct ones. public virtual…
rodbv
  • 5,214
  • 4
  • 31
  • 31
65
votes
5 answers

Can not instantiate proxy...Could not find a parameterless constructor

I am trying to create a unit test using Moq which tests the MongoDB.AspNet.Identity V2 provider. This line is giving me grief: var appUser = new Mock(); var userStore = new…
ChiliYago
  • 11,341
  • 23
  • 79
  • 126
62
votes
2 answers

Mock objects - Setup method - Test Driven Development

I am learning Test Driven Development and trying to use Moq library for mocking. What is the purpose of Setup method of Mock class?
meckrt
  • 765
  • 1
  • 5
  • 7
62
votes
4 answers

How to do internal interfaces visible for Moq?

I have 3 projects in my C# solution. Signatures Structures Tests Signatures have public and internal interfaces. Also, it has [assembly: InternalsVisibleTo("Structures")] [assembly: InternalsVisibleTo("Tests")] in AssemblyInfo.cs…
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
61
votes
4 answers

Moq: Setup a mocked method to fail on the first call, succeed on the second

What's the most succinct way to use Moq to mock a method that will throw an exception the first time it is called, then succeed the second time it is called?
anthony
  • 40,424
  • 5
  • 55
  • 128
61
votes
2 answers

How to add claims in a mock ClaimsPrincipal

I am trying to unit test my controller code which gets the information from the ClaimsPrincipal.Current. In the controller code I public class HomeController { public ActionResult GetName() { return…
Henry
  • 625
  • 1
  • 5
  • 6