Moq – Getting the Value Of a Mocked Method Parameter

moq logo

There’s scenarios where we don’t care about the Moq “It.IsAny<>” parameter value being passed into a mocked method but we need to get the value on the way out. For example, take the scenario below. I’m using SQLite to form integration tests and I need to get a handle on the generated ParentWidget.Id to tie back to my Widget FK. read more

Moq OData Delta Objects To Test Patch Methods

moq logo

Issue

Following on from my afternoon with Moq I stumbled upon another fun issue. How the hell could I test my API’s PATCH methods? They all make use of OData’s Delta objects. read more

Mock a return type of Task and Tuple with Moq

moq logo

Issue

Using Moq I was creating a few unit tests and stumbled across an interesting problem. I needed to mock a call to an async method on a service under test that took in a 2 parameters and returned a tuple of 2 values. read more

Moq – Mocking the same method call with multiple results

moq logo

The Scenario

I have a loop in production code that has evolved into processing batches of records. I need to modify an existing unit test to accommodate mocking the same method call each time through the loop returning different results on each loop (as it would in production).

The Solution

Using Moq we can make use of a Queue and Dequeue result sets each time in the loop as follows:

for (var i = 0; i < loop; i++)
{
var jobsInBatch = BuildJobs(batches).ToList();
jobQueue.Enqueue(jobsInBatch);
jobQueue.AddRange(jobsInBatch);
}

_processor.Setup(m => m.GetAllJobs(batches)).Returns(() => jobQueue.Dequeue());

Moq Vs Rhino Mocks

What follows is simply a few rough notes I made with experience with a Moq framework and converting to Rhino Mocks:

Asserting a method call

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

var person = new Person { name = "Bob" };

// Act
var something = new Something(mockedSomethingReporsitory);

// Assert
mockedSomethingRepository.AssertWasCalled(x => x.Save(person));

Asserting a property was set

As above but assert on the property….

mockedSomethingRepository.AssertWasCalled(x => x.WasPersonSaved = true);

Using a Stub to control program flow

Similar to Setup with Moq we’d use a Stub to return the desired flow i.e:

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

mockedSomethingRepository.Stub(x => x.ValidatePerson(Arg.Is.Anything)).Return(true));

Using a Stub to return the desired set on a property

ISomethingRepository mockedSomethingRepository = MockRepository.GenerateMock();

mockedSomethingRepository.Stub(x => x.IsValid).Return(true));

You can use GenerateStub here but the above setup for the stub keeps things syntactically the same in all mock setup instances.

Using Constraints to assert a method call with specific properties

In the first example we simply checked that a method was called. If we wanted to check the method was called passing in an object with specific properties we would do the following:

var personId = 23;
mockedSomethingRepository.AssertWasCalled(x => x.Save(Arg.Matches(x => x.Id == personId));

Check out some of my other Mocking tips and tricks….

Moq – Verifying parameter values on a mocked method call.

Moq – Mocking a cast on an interface