Is it possible to use Moq to mock an object that implements an interface and abstract class?
I.e.:
public class MyClass: SomeAbstractClass, IMyClass
Can you mock this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can mock any interface, and any abstract or virtual members. That’s basically it.
This means that the following are absolutely possible:
If the members inherited from SomeAbstractClass aren’t sealed, you can also mock MyClass:
Whether this makes sense or not depends on the implementation of MyClass. Let’s say that SomeAbstractClass is defined like this:
If the GetStuff method in MyClass is implemented like this, you can still override it:
This would allow you to write:
since unless explicitly sealed, GetStuff is still virtual. However, had you written GetStuff like this:
You wouldn’t be able to mock it. In that case, you would get an exception from Moq stating that it’s an invalid override of a non-virtual member (since it’s now
sealed).