Finally, I’ve started mocking things in our kind of legacy project while building new functionality. Now, I wanted to replace some DAL service with a mock that would would just serve me some results instead of ramping up NHibernate, going to the DB and assembling all the entities for this test. At first I tried to use FakeItEasy as I liked the syntax more than that of Moq:
1: var _geoObjectServiceMock = A.Fake<IGeoObjectService>();
2: var results = new List<int>{ 1, 2, 3, ... };
3: A.CallTo(() => _geoObjectServiceMock.GetSearchResultIds(A<GeoObjectSearchDesc>.Ignored))
4: .Returns(results);
This was easy. Just fix up the result you want the method to return and set it up. Well – I needed more. I needed to return my results list dependent on the GeoObjectSearchDesc parameter passed to the method. In short: I did not find a way to do this. I searched Google and Stackoverflow but to no avail. What I did find was an example using Moq showing exactly what I wanted to do. From Moq’s QuickStart wiki page:
1: // access invocation arguments when returning a value
2: mock.Setup(x => x.DoSomething(It.IsAny<string>()))
3: .Returns((string s) => s.ToLower());
4: // Multiple parameters overloads available
So I quickly installed Moq using NuGet, e.g. from the Package Manager Console:
1: Install-Package Moq -project Tests
Now I have something like this which is exactly what I was looking to do:
1: var _geoObjectServiceMock = new Mock<IGeoObjectService>();
2: var results = new List<int>{ 1, 2, 3, ... };
3: _geoObjectServiceMock.Setup(svc => svc.GetSearchResultIds(It.IsAny<GeoObjectSearchDesc>()))
4: .Returns<GeoObjectSearchDesc>(gosd => results.Take(gosd.PageSize).ToList());
That’s what I expected – to get control over the method parameter(s) inside the mock. Great! Thank you Moq.
Happy Coding,
Oliver