How can I Setup overloaded method invocations in Moq?
- by arootbeer
I'm trying to mock a mapping interface IMapper:
public interface IMapper<TFoo, TBar> {
TBar Map(TFoo foo);
TFoo Map(TBar bar);
}
In my test, I'm setting the mock mapper up to expect an invocation of each (around an NHibernate update operation):
//...
_mapperMock.Setup(m => m.Map(fooMock.Object)).Returns(barMock.Object);
_mapperMock.Setup(m => m.Map(barMock.Object)).Returns(fooMock.Object);
//...
However, when the second Map invocation is made, the mapper mock throws because it is only expecting a single invocation.
Watching the mapper mock during setup at runtime, I can look see the Map(TFoo foo) overload get registered, and then see it get replaced when the Map(TBar bar) overload is set up.
Is this a problem with the way Moq handles setup, or is there a different syntax I need to use in this case?