Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Moq-ing a class or interface?

I've been using moq to mock objects in my unit tests and I've seen on the site about moq that it is able to mock both classes and interfaces.

I had a discussion with one of my work mates the other day and they stated that there is never a reason to mock classes and I should only mock interfaces.

I didn't really have an answer to that....and I can't seem to find any answer to that on the moq site either.

Is it true that one should never mock classes? I'd say no since if that were true then Moq wouldn't even allow it....So then is there a time where it is better to mock a class over an interface? What is the difference between mocking a class vs mocking an interface? Or does it really just a preference thing?

like image 485
mezoid Avatar asked Dec 08 '08 12:12

mezoid


People also ask

How do you mock without an interface?

Simply mark any method you need to fake as virtual (and not private). Then you will be able to create a fake that can override the method.

Can we mock a interface?

We can use org. mockito. Mockito class mock() method to create a mock object of a given class or interface. This is really the simplest way to mock an object.

What is the difference between strict and loose mock?

When creating a mock, we can also give it strict or loose behavior. Strict behavior means that exceptions will be thrown if anything that was not set up on our interface is called. Loose behavior, on the other hand, does not throw exceptions in situations like this. Mocks, by default, are loose.

What is Moq used for?

What is minimum order quantity (MOQ)? Minimum order quantity is the fewest number of units a business wants to sell to a customer at one time. In ecommerce, it's most often used by a manufacturer or supplier in the context of a production run, though a merchant can put MOQs in place for different types of orders.


2 Answers

Mocking a class is perfectly valid. With MoQ, you can only mock virtual methods and properties on a class, though.

It's useful when you have abstract base classes instead of interfaces, or classes with default implementations and virtuals as extension points. There are many cases where you don't have access to the source code of classes you need to mock. There are many examples in the .Net framework, e.g. MembershipProvider.

like image 145
Mike Scott Avatar answered Oct 02 '22 20:10

Mike Scott


If the class does not implement an interface or if the mocking framework allows partial mocks then you would want to be able to mock the class. In the former case, because there is no interface to mock. In the latter, so you can inherit existing behavior from the class that you don't want to mock out.

Typically, though, you would want your classes to depend on interfaces when available. In that case, you needn't mock a class that implements the interface. You should just mock the interface itself to avoid any dependencies in your test that don't exist in the class under test.

like image 28
tvanfosson Avatar answered Oct 02 '22 18:10

tvanfosson