PartialMock<T> versus CallBase=true

863 views
Skip to first unread message

Tim Gifford

unread,
Aug 25, 2011, 12:32:22 AM8/25/11
to Moq Discussions
I've started working with Moq and really like the simplicity, clarity
and discoverability of the API. I had no problem discovering the
methods I needed until I ran into a situation that needed a partially
mocked concrete class. My natural instinct was to "new
PartialMock<T>()", but noticed it wasn't there. After searching I
learned about "CallBase = true" and it seamed out of step with the
discoverability of the framework.

My question (I apologize if it has been discussed before): Why not
create a PartialMock class that inherits from Mock and sets CallBase =
true in the constructor?

I'm willing to submit a patch, but wanted understanding and feedback
before doing the work.

Thanks,
Tim Gifford

Daniel Cazzulino

unread,
Aug 25, 2011, 12:42:29 PM8/25/11
to moq...@googlegroups.com
Because a "partial" mock doesn't make sense as a concept. You're just configuring the behavior of the mock. A simple property suffices there, just like DefaultValue, etc. Everything is a mock. Distinguishing a partial from a non-partial is pointless, just like distinguishing a Fake from a Stub from a Mock :)

At least IMHO ;)

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Brad Wilson

unread,
Aug 25, 2011, 12:55:35 PM8/25/11
to moq...@googlegroups.com
I know at some point in the past we talked about setting CallBase to true by default when you are mocking a class (as opposed to an interface). Did that ever happen?

Daniel Cazzulino

unread,
Aug 25, 2011, 1:39:12 PM8/25/11
to moq...@googlegroups.com

Nope, but its a good one

/kzu from Android

Chris Missal

unread,
Aug 25, 2011, 2:49:29 PM8/25/11
to moq...@googlegroups.com
Agreed, +1 from me fwiw. :)

Tim Gifford

unread,
Aug 25, 2011, 3:51:52 PM8/25/11
to moq...@googlegroups.com

Setting Callbase = true works for me too.

Tim Kellogg

unread,
Aug 26, 2011, 11:13:55 AM8/26/11
to moq...@googlegroups.com
Yeah, I think the most common expectation of a mocked class (concrete or not) is CallBase, if for no other reason than that it can be confusing to some people when some of their bases are called and others aren't (non-virtual methods are always called). If CallBase=true by default it would cut down on the visibility of this idiosyncrasy and make mocks just a little easier to understand :)

Although, this would be a pretty severe breaking change so it should be shipped with the next major version along with a warning to upgraders

--Tim

Daniel Cazzulino

unread,
Aug 26, 2011, 11:25:06 AM8/26/11
to moq...@googlegroups.com
Agreed 100%


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Mogens Heller Grabe

unread,
Nov 22, 2012, 2:26:13 AM11/22/12
to moq...@googlegroups.com
Was this never fixed?

I'm using Moq 4.0.10827, and my mocks of concrete classes aren't behaving like "partial mocks" by default, even though their methods and properties are virtual.

Is it really true that the most recent release of Moq was in Febryary 2011? Am I missing something?

Mogens Heller Grabe

unread,
Nov 22, 2012, 2:38:29 AM11/22/12
to moq...@googlegroups.com
Another reason why CallBase = true is more POLA compliant, is that when Equals and GetHashCode are defined to be virtual, they always return their default values (false and 0 respectively), leading to plain crazy behavior when using the mock in dictionaries, checking for its presence in lists, etc.

Daniel Cazzulino

unread,
Nov 22, 2012, 7:08:33 AM11/22/12
to moqdisc
You're not missing anything. It's THAT stable :P.

Seriously though, that's a breaking change that we'd need to evaluate deeper. It might break a bunch of tests for people that are relying on the old behavior. We'll see (they can always stay on the current version before the change).

The community is free to push moq to new limits. That's why it's open source. I moved it to github to ease the contribution process. We've already taken in a few, and we just need someone to help setting up a continuous integration build at myget.com and then we can push with one button new updates. 

If you like Moq, a great way to keep it moving is to contribute, of course.


/kzu

--
Daniel Cazzulino

Mogens Heller Grabe

unread,
Nov 22, 2012, 10:37:19 AM11/22/12
to moq...@googlegroups.com
Right!

Instead of changing the behavior alltogether, an alternative non-breaking approach could be if there was a possiblity to set up Moq's default behavior somewhere. 

I'm not familiar with any public APIs besides the Mock class, so this example might look pretty clumsy to Moq'ers - but something like a global 

    MockSettings.Default.CallBase = true; 

could do the trick. Thoughts?
Reply all
Reply to author
Forward
0 new messages