Is Moq too poweful?

It's interesting to see the other mocking frameworks as they grow into maturity. Especially Moq which has been getting loads of new features recently. The latest one is something which makes me feel a bit weird after reading all the things people fear in Typemock Isolator:It seems that  Moq now allows mocking protected fields.

What's really interesting is that Daniel has chosen to actively allow this, and hide the feature at the same time, as well as put it under a strict optional request:

you have to opt in for each code file where you'd like this ability by adding a special imports statement.

So, the questions I have are:

  • What happened to "if it's not testable it's a design smell"? Is this a compromise or have people figured out that good design can take many forms, with tools being left to do the hard lifting?
  • If you're going to add powerful "non classic" mocking ability to a framework, does adding it as a special "opt-in" feature make it OK? (this is what we are talking about doing with Typemock Isolator)
  • If tomorrow Rhino.Mocks were to add the same feature or something even more powerful (statics?) how would people treat that?
Published Sunday, July 06, 2008 2:59 PM by RoyOsherove

Comments

Sunday, July 06, 2008 4:57 PM by Steve B.

# re: Is Moq too poweful?

This whole thing (that Typemock and other tools like it leads to poor software design choices) seems to me to be a really odd position when it comes (as it often does) from the ALT.NET-leaning people.

One of the premises of ALT.NET (and in fact even the unofficial 'running with scissors logo' reinforces this) is that as a dev I am smart enough to make the right choices for myself and my project even if I am doing so using tools that offer little or no safety net.

To me, tools like TypeMock and others that support 'potentially' bad ways of working are perfectly fine in the hands of someone who knows what they are doing -- the idea that tools like TypeMock lead to 'lazy' developers writing poorly-structured and tightly-coupled code is just silly if you believe the initial premise: that TOOLS don't have to lead to good OR bad code but that its what you do with the tools that makes the difference.

Personally, as someone who has a significant part of their professional life spent on janitor duty (cleaning up the code messes left by other consultants, etc.), I applaud any move towards more powerful tools that allow me to work on things in ways I otherwise couldn't.

And that's true of TypeMock Isolator, MOQ, or any other tools that allow me to introduce workable tests into systems that otherwise would require blind refactoring and significant finger-crossing to safely improve the codebase as I and others work on it.

Sunday, July 06, 2008 5:25 PM by DotNetKicks.com

# Is Moq too powerful.

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Sunday, July 06, 2008 5:45 PM by Vadim

# re: Is Moq too poweful?

When I was learning TDD way, Typemock was my first mocking framework.  However, because I was new to TDD I made a lot of mistakes for which I had to pay later.  Typemock was letting me to mock almost anything and I was digging deeper and deeper by writing bad tests that I had to maintain later.

Now; however, I don't mind to switch back to Typemock because I know what can hurt me (at lest I think I know).

Power is not bad when you know how to control it.  But it can be dangerous for newbies.

Sunday, July 06, 2008 5:54 PM by Derik Whittaker

# re: Is Moq too poweful?

Turn about is fair play :).

I love it.

Sunday, July 06, 2008 6:22 PM by Nicholas Blumhardt

# re: Is Moq too poweful?

Roy, manually subclassing to create a test double allows for exactly the same behaviour.

Moq isn't giving you any power you don't already have in C#. This technique is pretty common outside of the use of mocking frameworks (it is even listed in xUnit Test Patterns.)

Don't think we're comparing apples with apples here :)

Nick

Sunday, July 06, 2008 8:45 PM by Chad Myers

# re: Is Moq too poweful?

Moq never made much sense to me. Daniel's mocking philosophy seemed to not be anywhere near where everyone else was on mocking. So, frankly, this doesn't really shock me and makes even less sense, I guess.

Ayende said that he'd accept a patch for static mocking if someone submitted it to Rhino.Mocks. Like you've said, Roy, there are times where you need it.

Scott Hanselman aptly described TypeMock as a 'crowbar' which is a perfect analogy, I think:

Amazingly useful when its use is appropriate, but rarely is its use appropriate and it can do  some damage if not used at the correct time.

(tech.groups.yahoo.com/.../11195)

My beef with TypeMock is not that it sells crowbars, it's that it sells crowbars as magic tools to solve all problems and that you should continue writing code that only crowbar-like tools can solve. This is harmful to the community, IMHO.

# Dew Drop - July 7, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - July 7, 2008 | Alvin Ashcraft's Morning Dew

Tuesday, July 08, 2008 12:33 AM by Daniel Cazzulino

# re: Is Moq too poweful?

Funny thing to say Chad, since all of a sudden things that were considered "at the core of mocking" (ayende.com/.../The-RecordReplayVerify-model.aspx) now are being copied from Moq (no explicit record-reply model), or the mixture of stub and mock behaviors in a single object, or the lambda-based syntax, etc. etc.

As Nicholas properly points out, mocking protected members is nothing you can't do with manually created mocks. Moq is there to help you avoid having to create those manual, so it made sense to support something that you can already do (and probably are doing) manually.

How else would you test that your protected members are being called and the base class is behaving as expected in that case with regards to its consumers? It's NOT the same as mocking statics.

I'm surprised that Roy is somehow comparing this to mocking statics "black magic"...

Depending on statics is bad. My guess is Moq will never support it, no matter how many patches I get to support it.

Yes, Moq is an "opinionated" framework at that. We want people to fall in the pit of success, not help them shoot their foot so that they learn how NOT to (precisely what Vadim went through).

Tuesday, July 08, 2008 12:55 AM by Daniel Cazzulino

# re: Is Moq too poweful?

BTW, Roy, Moq allows mocking protected properties and methods, NOT fields which cannot be mocked just as you can't override their get/set in your manual C# code.

Maybe that was missleading by my wording ("members"), but that's what the new feature is for.

We still continue to stay away from voodoo ;)

Tuesday, July 08, 2008 8:04 AM by Kent Boogaart

# re: Is Moq too poweful?

If Dan had allowed setting expectations on private members, then I'd be worried. The fact is, protected means "public to subclasses". Moq now lets you more easily test those subclass scenarios.

Wednesday, July 09, 2008 10:36 AM by Will

# re: Is Moq too poweful?

I just used this to assert that a private field was disposed property.  You can't do this using a mock unless you can access the internal (as the pattern dictates) virtual Dispose(bool) method.

MoQ FTW.

Thursday, July 10, 2008 6:49 PM by Troy DeMonbreun

# re: Is Moq too poweful?

My "running with scissors" comment is something I point out in my article on Test Supported Development:

blog.troyd.net/Test+Supported+Development+TSD+Is+NOT+Test+Driven+Development+TDD.aspx

"TypeMock seems to offer a shortcut around these interface extractions, but some TDDers might feel uncomfortable with the power of such a shortcut as it is incongruent with their valuing of TDD's IoC enforcement.

Developers should be free to choose to apply OO design idioms AS THEY SEE FIT. Obviously, good object-oriented design, refactoring, etc. is not exclusively the domain of TDD. Also, I would wager that if developers really wanted an IoC enforcement tool, they would prefer to go searching for (or create) just that [think SRP on a macro level], instead of having it forced upon them as part of a Development Methodology. In fact, a 'Design Principle Enforcement Tool' might not be a bad idea, assuming it does not yet exist."

Sunday, July 20, 2008 6:44 AM by Cokin Jack

# re: Is Moq too poweful?

Personally I think the discussion on TypeMock will eventually move on.

Currently everyone says IoC and mocking introduce good design practices, however many then use AMC and (where possible) automatic dependency hookup because really they're producing IFooService->FooService style interfaces.

Truth is good designers produce good designs, where "good" is totally dependent on the problem/environment and other factors

Leave a Comment

(required) 
(required) 
(optional)
(required)