AXMIM AXMIM - 1 year ago 116
C# Question

MOQ what happen if method setup intersect?

What happen when two setup intersect or overlap if you prefer.

For example, in the below scenario the setup overlap because obviously

is also considered as any string.

Interface ISomeInterface
int SomeMethod(string param);

public void SomeClass_ShouldBehaveProperly_GivenSomeScenario()
var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
mock.Setup(m => m.SomeMethod("aSpecificString"))
mock.Setup(m => m.SomeMethod(It.IsAny<string>()))

/*the rest of the test*/

I wonder what happen when it intersect.

Does it throw an exception or does it fail to detect the overlap and use the first matching setup in the order they were added?

I think it's best to avoid overlapping setup.

Answer Source

According to documentation found online, The last call wins and nullifies previous calls.

So even in your case if the test was to call


it would return 0 based on the setup in your example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download