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.

Print | posted on Wednesday, October 17, 2007 12:01 AM

Feedback


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 1:41 AM 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.


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 2:18 AM 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!


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 3:16 AM 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...


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 4:14 AM jdn

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


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 4:38 AM Luke Breuer

If you're talking about C# 3.0:
Expect.Call(() => mockServer.Start()).Throw(new InvalidConigurationException());


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/17/2007 7:25 AM 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.


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/22/2007 7:45 PM Christopher Bennage

w00t!


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/26/2007 8:23 PM ulu

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


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 10/26/2007 8:40 PM Ayende Rahien

Ulu,
That is unfortunately the compiler fault.
You would get this feature in VB9, though.


Gravatar

# re: Rhino Mocks: Void methods using Expect.Call 11/7/2007 7:16 PM Shane Courtrille

Shouldn't the example code actually say...

IServer mockServer = mocks.CreateMock<IServer>();

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.