Thursday 27 November 2008

Mocking Void Methods with Rhino Mocks

I’ve finally found a need to mock a method that has a void return option.  Here is how to do it with Rhino Mocks.

// Setup the delete methods
Expect.Call(delegate { items.Delete(_TestItems[0].Item); })
    .Throw(new System.Data.ConstraintException("duplicate row"))
    .Repeat.AtLeastOnce();

You can also have it return nothing and now throw any exceptions like so.

Expect.Call(delegate { items.Delete(_TestItems[1].Item); })
    .Repeat.AtLeastOnce();

No comments: