How can I Setup overloaded method invocations in Moq?

Posted by arootbeer on Stack Overflow See other posts from Stack Overflow or by arootbeer
Published on 2012-06-24T02:39:56Z Indexed on 2012/06/24 3:16 UTC
Read the original article Hit count: 2503

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about moq