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
166
votes
6 answers

Moq - Non-overridable members may not be used in setup / verification expressions

I'm new to Moq. I'm mocking a PagingOptions class. Here is how the class looks like: public class PagingOptions { [Range(1, 99999, ErrorMessage = "Offset must be greater than 0.")] public int? Offset { get; set; } …
fingers10
  • 6,675
  • 10
  • 49
  • 87
163
votes
3 answers

Using Moq to determine if a method is called

It is my understanding that I can test that a method call will occur if I call a higher level method, i.e.: public abstract class SomeClass() { public void SomeMehod() { SomeOtherMethod(); } internal abstract void…
Owen
  • 6,992
  • 7
  • 44
  • 77
160
votes
3 answers

How do I verify a method was called exactly once with Moq?

How do I verify a method was called exactly once with Moq? The Verify() vs. Verifable() thing is really confusing.
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148
156
votes
2 answers

What is the purpose of Verifiable() in Moq?

What is the purpose of Verifiable()? If I verify a Mock and leave this out it still verifies the SetUp. Edit: I was using VerifyAll() thus the reason for everything being verified. After changing to Verify() only my .Verifiable() SetUps were being…
Castrohenge
  • 8,525
  • 5
  • 39
  • 66
148
votes
2 answers

Moq, SetupGet, Mocking a property

I'm trying to mock a class, called UserInputEntity, which contains a property called ColumnNames: (it does contain other properties, I've just simplified it for the question) namespace CsvImporter.Entity { public interface IUserInputEntity …
Hans Rudel
  • 3,433
  • 5
  • 39
  • 62
145
votes
9 answers

How to mock ConfigurationManager.AppSettings with moq

I am stuck at this point of code that I do not know how to mock: ConfigurationManager.AppSettings["User"]; I have to mock the ConfigurationManager, but I don't have a clue, I am using Moq. Someone can give me a tip? Thanks!
Otuyh
  • 2,384
  • 5
  • 22
  • 40
141
votes
6 answers

How do I Moq a method that has an optional argument in its signature without explicitly specifying it or using an overload?

Given the following interface: public interface IFoo { bool Foo(string a, bool b = false); } Attempting to mock it using Moq: var mock = new Mock(); mock.Setup(mock => mock.Foo(It.IsAny())).Returns(false); gives the following…
Appulus
  • 18,630
  • 11
  • 38
  • 46
137
votes
6 answers

Can you help me understand Moq Callback?

Using Moq and looked at Callback but I have not been able to find a simple example to understand how to use it. Do you have a small working snippet which clearly explain how and when to use it?
user9969
  • 15,632
  • 39
  • 107
  • 175
129
votes
3 answers

Mocking objects with Moq when constructor has parameters

I have an object I'm trying to mock using moq. The object's constructor has required parameters: public class CustomerSyncEngine { public CustomerSyncEngine(ILoggingProvider loggingProvider, ICrmProvider…
Andrew Connell
  • 4,939
  • 5
  • 30
  • 42
118
votes
2 answers

Moq verify with object parameter

I am trying to verify a parameter that is a class. The code being tested is fine. The bug is in the test. I have tried two methods, both of which have failed. Here are my attempts: 1: this.MockImageResizeFilter.Verify(m =>…
rhughes
  • 9,257
  • 11
  • 59
  • 87
116
votes
3 answers

Moq: Invalid setup on a non-overridable member: x => x.GetByTitle("asdf")

Not sure how I can fix this, trying to do a unit test on the method "GetByTitle" Here are my definitions: public class ArticleDAO : GenericNHibernateDAO(IArticle, int>, IArticleDAO { public IArticle GetByTitle(string title) { IQuery…
mrblah
  • 99,669
  • 140
  • 310
  • 420
115
votes
6 answers

How do I mock the HttpContext in ASP.NET MVC using Moq?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock(); var request = new Mock(); context .Setup(c => c.Request) …
Geo
  • 8,663
  • 13
  • 63
  • 93
110
votes
16 answers

How to throw a SqlException when needed for mocking and unit testing?

I am trying to test some exceptions in my project and one of the Exceptions I catch is SQlException. It seems that you can't go new SqlException() so I am not sure how I can throw an exception especially without somehow calling the database (and…
chobo2
  • 83,322
  • 195
  • 530
  • 832
108
votes
6 answers

How to MOQ an Indexed property

I am attempting to mock a call to an indexed property. I.e. I would like to moq the following: object result = myDictionaryCollection["SomeKeyValue"]; and also the setter value myDictionaryCollection["SomeKeyValue"] = myNewValue; I am doing this…
Ash
  • 5,057
  • 7
  • 35
  • 49
106
votes
4 answers

Using Moq to mock only some methods

I have the following method: public CustomObect MyMethod() { var lUser = GetCurrentUser(); if (lUser.HaveAccess) { //One behavior } else { //Other behavior } //return CustomObject } I want to mock…
Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59