Unit Testing - Mock Objects

T

Terry

I'm looking at NMock2 as a framework to create mock objects during my unit
testing. One part that I'm trying to understand is that it will mock
interfaces, not concrete classes. I normally don't create interfaces for
every class. For example I normally don't have an interface for my business
object classes. A business object class is going to have too many unique
methods that I don't see the point of creating an interface that will only
ever have one class that implements it. That seems like overkill. However
if I want to mock the business object, it seems that with NMock2 I will need
to create an interface.

Am I understanding this correctly? Is my approach to interfaces wrong?

It looks like RhinoMock can mock interfaces or virtual methods of classes.
This raises a similar questions because I normally don't make everything
virtual. It seems like these frameworks would force me to change how I
design my classes to fit the frameworks. Is this right?

Any insights would be appreciated.

Thanks
Terry
 
K

Kevin Yu [MSFT]

Hi Terry,

Since NMock2 is a product from 3rd party software vendor, we may not be
able to provide you with useful information on this issue. In this case,
let's wait to see if any community member can shed some light on it.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

Jon Skeet [C# MVP]

Terry said:
I'm looking at NMock2 as a framework to create mock objects during my unit
testing. One part that I'm trying to understand is that it will mock
interfaces, not concrete classes. I normally don't create interfaces for
every class. For example I normally don't have an interface for my business
object classes. A business object class is going to have too many unique
methods that I don't see the point of creating an interface that will only
ever have one class that implements it. That seems like overkill. However
if I want to mock the business object, it seems that with NMock2 I will need
to create an interface.

Am I understanding this correctly? Is my approach to interfaces wrong?

It looks like RhinoMock can mock interfaces or virtual methods of classes.
This raises a similar questions because I normally don't make everything
virtual. It seems like these frameworks would force me to change how I
design my classes to fit the frameworks. Is this right?

Yes, that's pretty much right in my experience. However, I've found
that creating an interface makes it more obvious which parts of the
class are really required. Programming to that interface before you
have an implementation helps you to do design the class as you go,
which I find helpful. It also means you can test classes using that
interface before implementing it.

I certainly wouldn't like to try unit testing without having mocks
available :) I personally use EasyMock 2 in Java and a customised
version of EasyMock.NET for .NET. EasyMock.NET isn't likely to go
anywhere though - I'd suggest using RhinoMocks, from what I've seen of
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top