Stubbing existing classes doesn't work like expected

5 views
Skip to first unread message

Wigman

unread,
Jan 4, 2010, 9:40:32 AM1/4/10
to Rhino.Mocks
When stubbing a class i expect the original class code is not
executed. However, the following example does throw an exception("We
don't expect this to be thrown"). I thought stubs never failed a test.
What am I doing wrong here? How can i make Rhino stub the existing
class properly, so calling Test() does not give errors?

using System;
using NUnit.Framework;
using Rhino.Mocks;

namespace Roxit.Link.Test
{
[TestFixture]
public class RhinoTest
{
[Test]
public void TestStub()
{
Onzin onzin = MockRepository.GenerateStub<Onzin>();
onzin.Test();

}

public class Onzin
{
public void Test()
{
throw new Exception("We don't expect this to be
thrown");
}
}
}
}

Jonathon Rossi

unread,
Jan 4, 2010, 10:02:46 AM1/4/10
to rhino...@googlegroups.com
The Test method is not virtual, so RhinoMocks cannot override it to do nothing.


--

You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.





--
Jono

Wigman

unread,
Jan 4, 2010, 2:49:39 PM1/4/10
to Rhino.Mocks
Ok, now I know, thanx a million! Too bad i have to adjust the
'visibility' of my methods just to get the testing part in order.

> > rhinomocks+...@googlegroups.com<rhinomocks%2Bunsubscribe@googlegrou­ps.com>


> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rhinomocks?hl=en.
>
> --

> Jono- Hide quoted text -
>
> - Show quoted text -

Caio Kinzel Filho

unread,
Jan 4, 2010, 2:53:48 PM1/4/10
to rhino...@googlegroups.com
I think most of the time, when you are programming against interfaces
rather than against implementations, you will not have that problem,
once rhino.mocks can just implement that interface instead of
inheriting from your implementation

> To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.

Tim Barcz

unread,
Jan 4, 2010, 3:48:53 PM1/4/10
to rhino...@googlegroups.com
I'll be honest in that I don't run into this problem very often because I almost always try to have an interface in there...and as Caio pointed out, it's not a problem that shows up when programming interfaces.

Tim
--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

bill richards

unread,
Jan 4, 2010, 5:06:42 PM1/4/10
to Rhino.Mocks
and just to add to these perfectly explanatory responses .... this i
the result of NOT developing your code in a test driven manner

On Jan 4, 8:48 pm, Tim Barcz <timba...@gmail.com> wrote:
> I'll be honest in that I don't run into this problem very often because I
> almost always try to have an interface in there...and as Caio pointed out,
> it's not a problem that shows up when programming interfaces.
>
> Tim
>
> On Mon, Jan 4, 2010 at 1:53 PM, Caio Kinzel Filho <cai...@gmail.com> wrote:
>
>
>
>
>
> > I think most of the time, when you are programming against interfaces
> > rather than against implementations, you will not have that problem,
> > once rhino.mocks can just implement that interface instead of
> > inheriting from your implementation
>

> Tim Barcz
> Microsoft C# MVP

> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz- Hide quoted text -

Wigman

unread,
Jan 5, 2010, 2:39:37 AM1/5/10
to Rhino.Mocks
Well, excuse me, Bill. The Rhino documentation clearly states that
generating a stub will never fail a test. Well, it does! And there ARE
mock products that can actually create a thorough stub, stubbing all
methods of an existing class, virtual or not, so excuse me again for
my learning curve using Rhino. Saying test-driven developing means you
always create interface/virtuals for every class used in a test is new
for me too... and I sure hope you're wrong about it.

On Jan 4, 11:06 pm, bill richards <bill.richa...@greyskin.co.uk>
wrote:

> > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz-Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -

Tim Barcz

unread,
Jan 5, 2010, 7:28:55 AM1/5/10
to rhino...@googlegroups.com, Rhino.Mocks
It's it not that the stub is causing your test to fail ... It is that
the stub can't quite do its job until some prerequisites are met...the
one here that you are running into is that stubbed methods need to be
virtual ( this is so that dynamic proxy can proxy the method )

Tim

>>>>>>> %2Bunsubscribe@googlegrou ps.com>


>>>> <rhinomocks%2Bunsubscribe@googlegrou ps.com>
>>>>>>> .
>>>>>>> For more options, visit this group at
>>>>>>> http://groups.google.com/group/rhinomocks?hl=en.
>>
>>>>>> --
>>>>>> Jono- Hide quoted text -
>>
>>>>>> - Show quoted text -
>>
>>>>> --
>>
>>>>> You received this message because you are subscribed to the
>>>>> Google Groups
>>>> "Rhino.Mocks" group.
>>>>> To post to this group, send email to rhino...@googlegroups.com.
>>>>> To unsubscribe from this group, send email to
>>>> rhinomocks+...@googlegroups.com<rhinomocks

>>>> %2Bunsubscribe@googlegrou ps.com>


>>>> .
>>>>> For more options, visit this group at
>>>> http://groups.google.com/group/rhinomocks?hl=en.
>>
>>>> --
>>
>>>> You received this message because you are subscribed to the
>>>> Google Groups
>>>> "Rhino.Mocks" group.
>>>> To post to this group, send email to rhino...@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> rhinomocks+...@googlegroups.com<rhinomocks

>>>> %2Bunsubscribe@googlegrou ps.com>


>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/rhinomocks?hl=en.
>>
>>> --
>>> Tim Barcz
>>> Microsoft C# MVP
>>> Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://
>>> www.twitter.com/timbarcz-Hide quoted text -
>>
>>> - Show quoted text -- Hide quoted text -
>>
>> - Show quoted text -
>

> --
>
> You received this message because you are subscribed to the Google
> Groups "Rhino.Mocks" group.
> To post to this group, send email to rhino...@googlegroups.com.
> To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com

bill richards

unread,
Jan 5, 2010, 4:49:55 PM1/5/10
to Rhino.Mocks
Wigman,

from the tone of your response it seems that I have somehow offended
you. This was not my intention, I was merely attempting to highlight
the fact that had the code been develped in a TDD fashion you would
not have encountered this issue.

> > > Microsoft ASPInsiderhttp://timbarcz.devlicio.ushttp://www.twitter.com/timbarcz-Hidequoted text -

Reply all
Reply to author
Forward
0 new messages