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
544
votes
4 answers

Returning value that was passed into a method

I have a method on an interface: string DoSomething(string whatever); I want to mock this with MOQ, so that it returns whatever was passed in - something like: _mock.Setup( theObject => theObject.DoSomething( It.IsAny( ) ) ) .Returns(…
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
544
votes
8 answers

How to verify that method was NOT called in Moq?

How do I verify that method was NOT called in Moq? Does it have something like AssertWasNotCalled? UPDATE: Starting from Version 3.0, a new syntax can be used: mock.Verify(foo => foo.Execute("ping"), Times.Never());
alex
  • 74,215
  • 9
  • 49
  • 57
447
votes
5 answers

How can I tell Moq to return a Task?

I've got an interface which declares Task DoSomethingAsync(); I'm using MoqFramework for my tests: [TestMethod()] public async Task MyAsyncTest() { Mock mock = new Mock(); mock.Setup(arg =>…
Waldemar
  • 5,363
  • 3
  • 17
  • 28
401
votes
9 answers

Different return values the first and second time with Moq

I have a test like this: [TestCase("~/page/myaction")] public void Page_With_Custom_Action(string path) { // Arrange var pathData = new Mock(); var pageModel = new Mock(); var repository…
marcus
  • 9,616
  • 9
  • 58
  • 108
397
votes
15 answers

Assigning out/ref parameters in Moq

Is it possible to assign an out/ref parameter using Moq (3.0+)? I've looked at using Callback(), but Action<> does not support ref parameters because it's based on generics. I'd also preferably like to put a constraint (It.Is) on the input of the…
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
262
votes
8 answers

Mocking Extension Methods with Moq

I have a pre-existing Interface... public interface ISomeInterface { void SomeMethod(); } and I've extended this interface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface…
Russell Giddings
  • 8,731
  • 5
  • 34
  • 35
256
votes
5 answers

Verifying a specific parameter with Moq

public void SubmitMessagesToQueue_OneMessage_SubmitSuccessfully() { var messageServiceClientMock = new Mock(); var queueableMessage = CreateSingleQueueableMessage(); var message = queueableMessage[0]; var xml =…
Luis Mirabal
  • 2,606
  • 2
  • 16
  • 12
256
votes
4 answers

Using Moq to mock an asynchronous method for a unit test

I am testing a method for a service that makes a Web API call. Using a normal HttpClient works fine for unit tests if I also run the web service (located in another project in the solution) locally. However when I check in my changes the build…
mvanella
  • 3,456
  • 3
  • 19
  • 23
244
votes
17 answers

How to unit test with ILogger in ASP.NET Core

This is my controller: public class BlogController : Controller { private IDAO _blogDAO; private readonly ILogger _logger; public BlogController(ILogger logger, IDAO blogDAO) { …
duc
  • 2,585
  • 2
  • 14
  • 14
244
votes
24 answers

Mocking HttpClient in unit tests

I have some issues trying to wrap my code to be used in unit tests. The issues is this. I have the interface IHttpHandler: public interface IHttpHandler { HttpClient client { get; } } And the class using it, HttpHandler: public class…
tjugg
  • 2,977
  • 2
  • 18
  • 24
242
votes
8 answers

Moq: How to get to a parameter passed to a method of a mocked service

Imagine this class public class Foo { private Handler _h; public Foo(Handler h) { _h = h; } public void Bar(int i) { _h.AsyncHandle(CalcOn(i)); } private SomeResponse CalcOn(int i) { …
Jan
  • 6,532
  • 9
  • 37
  • 48
233
votes
1 answer

Verify a method call using Moq

I am fairly new to unit testing in C# and learning to use Moq. Below is the class that I am trying to test. class MyClass { SomeClass someClass; public MyClass(SomeClass someClass) { this.someClass = someClass; } …
user591410
  • 3,051
  • 5
  • 21
  • 30
199
votes
7 answers

Why am I getting an Exception with the message "Invalid setup on a non-virtual (overridable in VB) member..."?

I have a unit test where I have to mock a non-virtual method that returns a bool type public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, …
Rahul Lodha
  • 3,601
  • 7
  • 25
  • 34
188
votes
10 answers

How to mock the Request on Controller in ASP.Net MVC?

I have a controller in C# using the ASP.Net MVC framework public class HomeController:Controller{ public ActionResult Index() { if (Request.IsAjaxRequest()) { //do some ajaxy stuff } return…
Nissan
  • 1,985
  • 2
  • 13
  • 11
167
votes
3 answers

Moq mock method with out specifying input parameter

I have some code in a test using Moq: public class Invoice { ... public bool IsInFinancialYear(FinancialYearLookup financialYearLookup) { return InvoiceDate >= financialYearLookup.StartDate && InvoiceDate <=…
AnonyMouse
  • 18,108
  • 26
  • 79
  • 131
1
2 3
99 100