Moq (pronounced "Mock-you" or just "Mock") is a mocking framework for .NET that makes heavy use of lambdas and LINQ expression trees. This tag is specific to Moq version 3.0.
Questions tagged [moq-3]
36 questions
20
votes
1 answer
Subsequent calls to a Mock.Setup result in the same object instance
I am setting up a Mock as shown below. It is passed into the constructor of the target. The target has a Decrypt method that is called twice within the lifetime of the target. Each time the Decrypt method is called, it Disposes of the certificate…

Pete Maroun
- 2,045
- 2
- 18
- 27
18
votes
4 answers
Verify value of reference parameter with Moq
I just switched to Moq and have run into a problem. I'm testing a method that creates a new instance of a business object, sets the properties of the object from user input values and calls a method (SaveCustomerContact ) to save the new object. …

Simon Calvin
- 513
- 1
- 4
- 11
16
votes
1 answer
When using Moq Verify() method invocation count, have failing test's error message contain actual method invocation count using Moq
Consider the following, where I am testing that an injected dependency's method is called a specific number of times:
[Fact]
public void WhenBossTalksEmployeeBlinksTwice()
{
// arrange
var employee = new Mock();
…

Jeff
- 2,191
- 4
- 30
- 49
14
votes
2 answers
MOQ stubbing property value on "Any" object
I'm working on some code that follows a pattern of encapsulating all arguments to a method as a "request" object and returning a "response" object. However, this has produced some problems when it comes to mocking with MOQ. For example:
public…

bytedev
- 8,252
- 4
- 48
- 56
12
votes
1 answer
How to Mock ILogger / ILoggerService using Moq
I'm writing some unit tests for my View Model class. The constructor of this class is injected with an ILoggerService. This interface defines 1 method GetLog which returns an ILogger. Something like below where this represents a class that…

user630190
- 1,142
- 2
- 11
- 26
6
votes
3 answers
how do you mock an xml for unit testing?
I need to unit testing this GetData method.
public MessageResponse GetData(XmlElement requestElement)
{
MessageResponse MsgResponse = new MessageResponse();
if (requestElement.Attributes["employeeNo"] == null){
…

qinking126
- 11,385
- 25
- 74
- 124
3
votes
1 answer
Using Moq : Mock object update automatically?
I am new to MoQ framework. I am writing unit testing for controller using MoQ framework and here is my test method,
var mockedItemDetail = new ItemDetail()
{
Name = null
};
var mockObject = new Mock();
…

Praveen
- 831
- 7
- 15
3
votes
2 answers
How to mock a DisplayMode in ControllerContext For Unit Test c#
I want to test an action in my controller that use a controllerContext as a parameter to generate a pdf document based on a 3th part library "Rotativa".
Here is the implementation of the action (function) :
public ActionResult DetailsPrint(int?…

Le-Mr-Ruyk
- 179
- 2
- 17
3
votes
3 answers
How can I create a mocked object inside a mocked object?
I've got these two lines on my code.
Customer customer = Repository.Customer.GetById(customerId);
Employee employee = customer.Employees.Single(e => e.IsPrimaryContact);
Now, I'm creating a unit test. Both Objects, customer and employee are…

choopau
- 2,209
- 5
- 21
- 28
3
votes
1 answer
Original method still getting called in Moq even after CallBase = true/false
Here's my code:
public class Bar { }
public class Foo { public string Name { get; set; } public Bar TheBar { get; set; } }
public class Dependency
{
public Foo DoSomething(Expression> exp1) { return new Foo(); }
}
public class…

kuzzanagi
- 55
- 1
- 1
- 7
3
votes
1 answer
AutoFixture with AutoMoq and concrete object injection
I'm facing a strange problem related to AutoFixture and AutoMoqCustomization and how it deals with automocking of concrete classes. I suspect that I'm not using it very well but would like to know what's the problem. First of all her's some context.…

Tomasz Jaskuλa
- 15,723
- 5
- 46
- 73
3
votes
2 answers
Can I use Moq in this situation?
What possible work-around do I have for mocking this object using Moq. The error message returned is:
"Invalid setup on a non-virtual (overridable in VB) member: p => p.Certificate.SubjectName.Name"
var st = new Mock();
…

Pete Maroun
- 2,045
- 2
- 18
- 27
2
votes
1 answer
How to mock a method that is called multiple times in single call using Moq in C#.NET?
I am trying to mock the below method but the updateRefundReqeust returns null instead updated record.
public async Task InvokeAsync(Batch batch)
{
var refundRequests = await…

Bijay Yadav
- 928
- 1
- 9
- 22
2
votes
1 answer
Using count method
I am quiet new to Moq Framework and unitests. my goal is to insert some info to the mock object, and want to be able to count the number of objects inserted with a current value.
I build my mock the in the following way:
var mockCpmSqlDbContext =…

Dan The Man
- 1,835
- 6
- 30
- 50
2
votes
1 answer
How to pass a List parameter using Moq
I am trying to mock a method which takes two parameters, the signature of method looks as follows:
User DoSomething(User user, List newRoleList);
I want this method to return something only if the 'newRoleList' parameters contains some…

binu
- 337
- 2
- 5
- 16