Moq'ing base class

1,643 views
Skip to first unread message

martin_ke...@yahoo.com.au

unread,
Feb 3, 2009, 2:56:22 PM2/3/09
to Moq Discussions
Hello,
I want to test a class that inherits from a base class.

The base class is based on IRouteHandler.
The method GetHttpHandler is called in the derived class. The first
thing it does is invoke the base class which does lots of complex work
to change the requestcontext.

My derived class then uses the data in the requestcontext to "do
stuff"

Rather than set up the data for the base class to work - I want to moq
it.
The problem - I cant change the GetHttpHandler signature so I cant
pass down a moq'ed base class through a parmeter change. If I added an
overload to the derived class then I would have to write code in my
derived class to point to the passed down base class - that isnt unit
testing.

Is there anyway I can make the "inherited" base class in the derived
class look at the moq instead of the base.

Thanks
Martin

Daniel Cazzulino

unread,
Feb 3, 2009, 4:24:19 PM2/3/09
to moq...@googlegroups.com
if the method is virtual, just doing a setup for it will prevent the call to the base class.

alternatively, you can do:

            var mock = new Mock<MyClass>() { CallBase = false };

which would prevent calls to the base class for all virtual members.
--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471

martin_ke...@yahoo.com.au

unread,
Feb 3, 2009, 4:50:14 PM2/3/09
to Moq Discussions
Hello,
is setup new - its not in v2.6 documenation?

Can you give me more of an example please - I dont follow you

in my example I have

class baseHandler : iRouteHandler
{
public IHttpHandler GetHttpHandler( RequestContext requestContext)
{
Do lots of stuff to requestContext
}
}

class derivedHandler : baseHandler
{
public IHttpHandler GetHttpHandler( RequestContext requestContext)
{
base.GetHttpHandler(requestContext);
do lots more stuff
}
}

I want to Test derivedHandler and mock basehandler to just return
null.


are you saying

var mock = new Mock<basehandler>() { CallBase = false };
but then how do I inject the mock into derivedhandler.GetHttpHandler's
to replace the call to base.GetHttpHandler(requestContext); ?



thanks for your help with this
Martin
> 425.329.3471- Hide quoted text -
>
> - Show quoted text -

Daniel Cazzulino

unread,
Feb 5, 2009, 9:40:03 PM2/5/09
to moq...@googlegroups.com
setup in v3.0 == expect in v2.6

You cannot do with Moq what you cannot do with C#.

In C# there's no way you can inherit from DerivedHandler (in order to mock it) and somehow override BaseHandler but not DerivedHandler's version of a single method.

My suggestion would be to first figure out how you would be able to do that with plain C# (i.e. providing the derived class behavior in a different virtual method? Or having the base behavior in another method?). Once you can achieve that with plain C#, then Moq can do the same for you without creating manually the mock classes.

HTH!

/kzu


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


mken...@gam.com

unread,
Feb 6, 2009, 6:49:20 AM2/6/09
to Moq Discussions
Hello,
it means using OO inheritance is out if you want to moq then.

thanks for your time
Martin
> > > - Show quoted text -- Hide quoted text -

Geoff Taylor

unread,
Feb 6, 2009, 8:19:51 AM2/6/09
to moq...@googlegroups.com
It's probably not fair to pick on Moq for that - it's a general consequence
of inheritance as implemented in .NET (and Java), and I don't think you'd
have more success with Rhino Mocks. It would probably be truer to say
"using OO inheritance is out if you want to mock then".

You might have some success with your specific problem if you have a look at
some Inversion of Control pattern implementations. Also, a search for "mock
httpcontext" might be useful - I have an inkling that someone posted some
code for that problem that might apply to your solution (it wasn't in this
group, and I can't find the link now I'm afraid).

Good luck!

Geoff

Daniel Cazzulino

unread,
Feb 6, 2009, 8:23:49 AM2/6/09
to moq...@googlegroups.com
quite the contrary.
it means if you can't do it with straight OO inheritance, then you can't do it with moq.

the scenario you laid out first is not possible with straight OO inheritance (inheriting a method, calling the base, but somehow having the base class behavior while changing/overwriting the base-base class behavior).



/kzu

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


mken...@gam.com

unread,
Feb 6, 2009, 8:53:38 AM2/6/09
to Moq Discussions
My scenario is a derived class method is called , it calls its base
class'es method.
This is perfectly normal and correct for OO.

the issue comes with unit testing. rather than setting up a lot of
data so the base handler throws the correct value to the derived
class , all I wanted to do was moq the baseclass.

I accept that moq is limited by what you can do in c# (fair enough -
thanks for telling me that - i think moq ia almost a back art at times
so this is kind of reassuring <g>)

but apart from the unit testing implications , what I am doing is
pretty standard OO stuff.

My solution is to set up the test data so the base handler returns
what its supposed to - I have other unit tests on the base class
proving that)


Thanks for the help



On Feb 6, 1:23 pm, Daniel Cazzulino <kzu....@gmail.com> wrote:
> quite the contrary.
> it means if you can't do it with straight OO inheritance, then you can't do
> it with moq.
>
> the scenario you laid out first is not possible with straight OO inheritance
> (inheriting a method, calling the base, but somehow having the base class
> behavior while changing/overwriting the base-base class behavior).
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>
>
>
> On Fri, Feb 6, 2009 at 9:49 AM, <mkendr...@gam.com> wrote:
>
> > it means using OO inheritance is out if you want to moq then.- Hide quoted text -

Daniel Cazzulino

unread,
Feb 6, 2009, 9:33:10 AM2/6/09
to moq...@googlegroups.com
I don't think I follow.
If I understood correctly, your scenario was:

public class BaseClass
{
   public virtual void DoSomething()
   {
      // do some stuff
   }
}

then on the derived class:

public class DerivedClass : BaseClass
{
   public override void DoSomething()
   {
      // do lots of stuff
      base.DoSomething();
   }
}

And, again, if I understood correctly, you were asking how you could *overwrite* the behavior on DerivedClass, while still calling the BaseClass behavior, something like:

public class MockClass : DerivedClass
{
   public override void DoSomething()
   {
      // hardcode some values/behavior here.
      // And invoke the BaseClass.DoSomething WITHOUT invoking DerivedClass behavior!
      base.base.DoSomething(); // ??
   }
}

Maybe I completely misunderstood the issue, but this is absolutely impossible with C# OO.
You simply CAN NOT override a base class behavior and invoke an ancestor up the inheritance chain WITHOUT having your overriden behavior kick-in the moment you do base.Call()



/kzu

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


martin_ke...@yahoo.com.au

unread,
Feb 6, 2009, 12:44:43 PM2/6/09
to Moq Discussions
hey
no I wasnt asking that, I effectively wanted


public class BaseClass
{
public virtual void DoSomething()
{
// do some stuff
}

}

Create a Moq of Base class

then on the derived class effectively

public class DerivedClass : BaseClass
{
public override void DoSomething()
{
// do lots of stuff
MoqOfBaseClass.DoSomething(); // This is still defined in the
code as Base.DoSomething but the Moq takes over
}

}

thanks
> > > - Show quoted text -- Hide quoted text -

Daniel Cazzulino

unread,
Feb 6, 2009, 2:35:30 PM2/6/09
to moqdisc
But what you're saying is exactly what I am saying.
You want DerivedClass behavior, while changing the BaseClass behavior (which should go to the mock).

Can't do that with C# unless you refactor the behavior and introduce new virtuals.


/kzu

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


2009/2/6 martin_kendrick_uk <martin_ke...@yahoo.com.au>

martin_ke...@yahoo.com.au

unread,
Feb 6, 2009, 3:28:44 PM2/6/09
to Moq Discussions
ok
thanks for your help with this.
Martin
Reply all
Reply to author
Forward
0 new messages