Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Mock return a new list every time the method is called using Moq

Tags:

c#

.net

mocking

moq

I'm using MOQ to mock a method call with an expected return list. My method returns a list but i want the mock to make a new list every time the method gets called. What I've done so far:

List<Correlation> expected = new List<Correlation> { new Correlation() { Code = "SelfError1" }, new Correlation() { Code = "SelfError2" } };
Mock<IRPLValidator> selfMock = new Mock<IRPLValidator>();
selfMock.Setup(f => f.Validate()).Returns(expected);

What I'm trying to achieve is to make the mock return a new list every time the method get's called. I've tried this but didn't work:

selfMock.Setup(f => f.Validate()).Returns(new List<Correlation>{ new Correlation() { Code = "SelfError1" }, new Correlation() { Code = "SelfError2" } });

As this didn't worked, I'm thinking maybe Callback is the answer to my question but I didn't find any proper example for reinitializing my list. Any suggestions?

As you may wonder why do I need a new list every time, the problem is that I'm calling the method on different object types making some changes in the list, depending on the object type. Because the mock gives me the same list every time the method is called, I'm always modifying the same object in the memory thus I can't keep track of the changes I'm making on it.

Thanks in advance!

like image 803
Florin Bombeanu Avatar asked Nov 08 '11 14:11

Florin Bombeanu


People also ask

How do you mock a method in MOQ?

First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.

What is CallBase in MOQ?

CallBase , when initialized during a mock construction, is used to specify whether the base class virtual implementation will be invoked for mocked dependencies if no setup is matched. The default value is false . This is useful when mocking HTML/web controls of the System.

What is callback in MOQ?

A callback is a piece of code that is passed into to a method parameter to be executed from within that method. Callbacks allow you to extend the functionality of such methods. When using Moq to create test doubles, you can supply callback code that is executed when an expectation is met.

What is verifiable in MOQ?

Verifiable is to enlist a Setup into a set of "deferred Verify(...) calls" which can then be triggered via mock. Verify() .


1 Answers

selfMock.Setup(f => f.Validate()).Returns(() => new List<Correlation>{ new Correlation() { Code = "SelfError1" }, new Correlation() { Code = "SelfError2" } });

You need to turn the value inside the Returns into a function. The version you tried created a single list, then returns just used that. This way, the list is created each time as the returns will call the function each time it is needed.

like image 186
Rangoric Avatar answered Sep 29 '22 18:09

Rangoric