How can I apply the void method to return a Void from a Stub?

How can I make the stub object in RhinoMocks return void for the void method?

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 ? 
+6
source share
2 answers

The method cannot return a value - this is the void method. The CLR will not allow it to return a value. You do not need to check this.

You only need an Expect call.

+8
source

The Return() method is not valid for calling the void method. Rather, you want something like this:

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

which will tell you if Horn() called.

The way you test that void methods are called during unit testing. You do the following:

  • Set Expectation ( Expect() )
  • Call the method that should be waiting
  • Verify that the expected method is being called.
+6
source

Source: https://habr.com/ru/post/899058/


All Articles