Is there a way to get my mocks to 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
is not overrideable.
Any suggestions?
Is there a way to get my mocks to 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
is not overrideable.
Any suggestions?
Instead of using the is
operator to check types, you could (not should) implement your own overridable interface method that performs a similar function, and implement it with the is
operator (or typeof()/GetType()
) on your usual bunch of classes.
That said, if you're using the is
operator in a way that needs to be testable like this, it's more likely than not that you're basically defeating the purpose of polymorphism and interfaces somewhere along the line. I'd think about whether I could just get rid of it.