pencilCake
pencilCake

Reputation: 53323

How can I enforce a void method to return Void from a Stub object?

How can I enforce a stub object in RhinoMocks to return void for a void method on it?

Take this example:

public interface ICar 
{
    string Model {get;set;}
    void Horn();
}

ICar stubCar= MockRepository.GenerateStub<ICar>();
stubCar.Expect(c=>c.Horn()).Return( //now what so that 
                                   // it returns nothing as the meth. returns void ? 

Upvotes: 6

Views: 5499

Answers (2)

George Stocker
George Stocker

Reputation: 57907

The Return() method is invalid for a void method call. Rather you want something like this:

ICar stubCar= MockRepository.GenerateStrictMock<ICar>();
stubCar.Expect(c=>c.Horn());
stubCar.DoSomethingThatIsSupposedToCallHorn();
stubCar.VerifyAllExpectations();

which will tell you whether or not Horn() was called.

That's how you test that void methods are called when unit testing. You do the following:

  1. Set up an Expectation (Expect())
  2. Call the method that should invoke the expectation
  3. Verify that the expected method was called.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1503924

The method can't return a value - it's a void method. The CLR won't let it try to return a value. You don't need to test for this.

You only need the Expect call.

Upvotes: 9

Related Questions