Mock same interface as 2 different types

I have an interface IMyInterfacethat I mock unit test with moq.

Mock<IMyInterface> firstMockedObject = new Mock<IMyInterface>();
Mock<IMyInterface> secondMockedObject = new Mock<IMyInterface>();

The device under test has a registration method that looks like this:

public void RegisterHandler(Type type, IHandler handler)

and then the descriptor method:

public void Handle(IMyInterface objectToHandle)

What I'm trying to check is that I can regiester 2 handlers for two different implementations IMyInterfaceand that the Handle method correctly selects which one to use:

UnitUnderTest.RegisterHAndler(firstMockedObject.Object.GetType(), handler1);
UnitUnderTest.RegisterHAndler(seconMockedObject.Object.GetType(), handler2);

Problem - both mock objects of the same type. Is there a way to get Moq to generate 2 layouts of the same interface as different types?

+4
source share
2 answers

Createtwo, . . :

public interface IMockOne : IMyInterface { };
public interface IMockTwo : IMyInterface { };


var firstMockedObject = new Mock<IMockOne>();
var secondMockedObject = new Mock<IMockTwo>();

, moq .

+6

.

public class MockOne : IMyInterface {}
public class MockTwo : IMyInterface {}
+1

Source: https://habr.com/ru/post/1617281/


All Articles