Mock GetType() Metod

2,042 views
Skip to first unread message

alamandra007

unread,
Mar 11, 2008, 5:05:29 PM3/11/08
to Rhino.Mocks
I'm new to RM and I was Just wondering how to mock the GetType()
metod, let's say for SqlConnection class. Could you help me please?

Cheers,
alamandra007

Ayende Rahien

unread,
Mar 12, 2008, 12:39:28 PM3/12/08
to Rhino...@googlegroups.com
You can do that only with remoting mocks, which are not always suitable.
What is the scenario that you are after?

alamandra007

unread,
Mar 12, 2008, 4:43:46 PM3/12/08
to Rhino.Mocks
Suppose I've got got method SomeMethod inside tested class, looking
like

void SomeMethod(object c)
{
if (c.GetType() == typeof(SqlConnection))
{
// ..some code that wan't invoke but should
}
}

I need the code inside brackets to invoke, but it won't. Unless I mock
the GetType method, while the code of SomeMethod can not be modified.
Is it possible?

Patrick Steele

unread,
Mar 12, 2008, 5:22:35 PM3/12/08
to Rhino...@googlegroups.com
I think you'd rather do:

if(c is IDbConnection)
{
...

alamandra007

unread,
Mar 12, 2008, 5:50:34 PM3/12/08
to Rhino.Mocks
I said that I can not modify SomeMethod method.

Patrick Steele

unread,
Mar 12, 2008, 5:52:39 PM3/12/08
to Rhino...@googlegroups.com
Oops. Sorry, missed that.

alamandra007

unread,
Mar 12, 2008, 6:06:22 PM3/12/08
to Rhino.Mocks
That's ok:) Nobody's perfect!

alamandra007

unread,
Mar 13, 2008, 1:21:58 PM3/13/08
to Rhino.Mocks
I get the InvalidOperationException with message "Can set only a
single return value or exception to throw or delegate to execute on
the same method call", when i try to mock the method at line

Expect.Call(this.Target.GetType()).Return(this.GetType());

where this.Target is a mocked SqlConnection instance.

Ayende Rahien

unread,
Mar 13, 2008, 4:03:55 PM3/13/08
to Rhino...@googlegroups.com
That is because GetType() cannot be mocked using normal mocks.
You can use remoting mocks, but I _STRONGLY ADVICE_ you to avoid this.

The code that you have at the moment should be changed, and the tests are showing you this.

Owen Evans

unread,
Mar 13, 2008, 4:10:52 PM3/13/08
to Rhino...@googlegroups.com
I would agree on Ayende's point really do advise avoid. Ask why you are testing a class you cannot modify (you stated you can't modify the call to gettype) and instead try and abstract away the code that is fixed and use an adapter or factory facade so that you can just remove the test reliance on untestable code

my 2 cents

O

alamandra007

unread,
Mar 13, 2008, 4:49:59 PM3/13/08
to Rhino.Mocks
While I'm still not convinced, why remoting mocks are such a bad idea,
could you please briefly describe the reasons for not using them?

Ayende Rahien

unread,
Mar 13, 2008, 4:51:49 PM3/13/08
to Rhino...@googlegroups.com
The need to mock GetType() is a bad idea.

On 3/13/08, alamandra007 <alaman...@gmail.com> wrote:

alamandra007

unread,
Mar 13, 2008, 4:55:34 PM3/13/08
to Rhino.Mocks
OK, I think I'm starting to understand your point of view:D

alamandra007

unread,
Mar 13, 2008, 5:04:55 PM3/13/08
to Rhino.Mocks
Mock frameworks should be as transparent as they can only be. This has
some serious cosequences. But that's my point of view.
Reply all
Reply to author
Forward
0 new messages