Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a unit test immediately after an expected method is called on the mock object?

When working with Rhino.Mocks with a mock object in hand:

Is there a way to pass a unit-test once an expected method is called without executing the lines after the call to this expected method?

Thanks

like image 854
pencilCake Avatar asked Oct 25 '11 14:10

pencilCake


1 Answers

Since RhinoMocks 3.5 you can use nice AssertWasCalled()

this.Service.BuldMessage("messageId");
this.Service.AssertWasCalled(x => x.GenerateMessage("messageId"), messageId));
  • Beautiful (nontrivial) Code - Rhino Mocks 3.5's AssertWasCalled

EDIT: Answer to comment

RhinoMock is not in charge to change test execution flow, so you have to use NUnit asserts, Assert.Pass() utility method allows you to immediately end the test, recording it as successful:

if (this.Service.AssertWasCalled(...)))
{
   Assert.Pass("well done");
}

PS: as others suggested consider redesing of unit test which forced you to do such conditional test exit.

like image 151
sll Avatar answered Sep 28 '22 09:09

sll