Ayende @ Rahien

Refunds available at head office

Rhino Mocks: Void methods using Expect.Call

One of the things that I dislike about Rhino Mocks is the disconnect between methods that return a value and methods that do not. The first are handled quite naturally using Expect.Call() syntax, but the later have to use the LastCall syntax, which is often a cause of confusion.

There is little to be done there, though, that is a (valid) constraint place there by the compiler, can't do much there, right?

Jim Bolla had a different idea, and had a great suggestion, so now we can write this:

IServer mockServer = mocks.CreateMock<IMock>();
Expect.Call(delegate { mockServer.Start(); }).Throw(new InvalidConigurationException());
// rest of the test

It is also a good way to start preparing Rhino Mocks for C# 3.0.

Many thanks to Jim.

Again, the code is in the repository, and will be released soon.

Comments

Jason Meridth
10/16/2007 11:41 PM by
Jason Meridth

It's scary how my colleague and I were just talking about this today.

Thanks for the post. It will be very beneficial.

Sean Chambers
10/17/2007 12:18 AM by
Sean Chambers

Excellent! I often dislike having to use the LastCall syntax as well. This makes all of my expectations look the same. I like it!

Robert Ream
10/17/2007 01:16 AM by
Robert Ream

Perfect, I was just thinking the same thing last night too, after writing that 1 millionth mocked void method call expectation this project...

jdn
10/17/2007 02:14 AM by
jdn

I was just dealing with this again today, this is great.

Luke Breuer
10/17/2007 02:38 AM by
Luke Breuer

If you're talking about C# 3.0:

Expect.Call(() => mockServer.Start()).Throw(new InvalidConigurationException());

Dave Newman
10/17/2007 05:25 AM by
Dave Newman

Sweet!

Man I can't wait to get in to c# 3. Of course until resharper supports it it's pretty much a no go.

ulu
10/26/2007 06:23 PM by
ulu

I guess we VB programmers are out of luck again..

Ayende Rahien
10/26/2007 06:40 PM by
Ayende Rahien

Ulu,

That is unfortunately the compiler fault.

You would get this feature in VB9, though.

Shane Courtrille
11/07/2007 05:16 PM by
Shane Courtrille

Shouldn't the example code actually say...

IServer mockServer = mocks.CreateMock();

Expect.Call(delegate { mockServer.Start(); }).Throw(new InvalidConigurationException());

// rest of the test

Because the current example creates a mock of IMock without showing any relation between it and IServer.

Comments have been closed on this topic.