Mocking operator in Moq

Is there a way to make my layouts impersonate a type? I am trying to do something like this:

var myMock = new Mock<IMyType>(); myMock.Setup(x => x.GetType()).Returns(typeof(MyTypeImpl)); 

however, GetType not overridden.

Any suggestions?

+4
source share
2 answers

Instead of using the is operator for type checking, you can (should not) implement your own override interface method that performs a similar function and implement it with the is (or typeof()/GetType() ) operator on your regular class group.

However, if you use the is operator in such a way that it can be tested in this way, you are most likely defeating the goal of polymorphism and interfaces somewhere along the line. I would think if I could just get rid of it.

+7
source

I know this is an old post, but I was looking for a solution to this problem ...

Using Moq, you can add a standard GetType signature to your interface, allowing Moq to mock this method without having to write more code, as this method is already implemented for you.

 Type GetType(); 
+3
source

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


All Articles