Archives for: February 2012

02/17/12

Permalink 08:14:42 pm, by truewill Email , 229 words, 5231 views   English (US)
Categories: Testing, IoC, .NET, Design

How can I mock X?

I’m on Stack Overflow often, and I follow the [moq] tag. I love Moq, and can answer a fair number of questions about it.

The questions that annoy me are ones like this:

How can I moq this query against ravendb?

Don’t.

You’ve just coupled your application to the library. Yes, IDocumentSession is an interface. But it’s RavenDB’s interface. Does the class that you’re injecting it into call all of its methods? Does it use all of the properties of the interfaces returned by these methods? I doubt it.

You’re violating the Interface Segregation Principle.

Your class is also announcing, “I know I’m talking to a NoSQL database, or something like one, and that that database’s API looks exactly like RavenDB’s.”

It’s not just RavenDB. HttpContext, repositories using IQueryable<T>, interfaces of expressions of generic function delegates, you name it.

Does your class need all of that functionality?

Or does it just need a simple service?

Custom interfaces are easy to write, easy to implement (with a custom wrapper class), and easy to mock. They let you be explicit in your dependencies, specifying what a given class needs and no more. With a custom interface, you can also replace the actual implementation easily; you’re no longer wedded to a given library, technology, or product.

Time spent thinking about dependencies is time saved asking questions about mocking frameworks.

Development Central

Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.

Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.

Search

Categories

Linkblog

b2evolution

contributors

XML Feeds

What is RSS?

Who's Online?

  • Guest Users: 49

powered by b2evolution free blog software