Search code examples
c#unit-testingrhino-mocksfakeiteasy

Rhino Mocks 'Expect' with FakeItEasy


I have a database manipulating object as a dependency of my UUT (Unit Under Test). Therefore, I want to make it as a strict mock, because I also want to make sure that the UUT does not call any other methods that can result db change.

In rhino mocks I did the following:

  1. I made a strictmock from the db object
  2. I made an .Expect clause in Arrange
  3. I called VerifyAllExpectations in Assert

However, when I want to do this in FakeItEasy, I can't find how to do it without code duplication. I tried putting the CallsTo()+MustHaveHappened() parts in the Arrange, but then my test fail. If I put the CallsTo()+MustHaveHappened() parts in the Assert, then my test fail also, because unexpected calls were made to a strict fake. Can this be done without putting the CallsTo call into both Arrange and Assert?


Solution

  • You can achieve that with following verifications:

    var service = A.Fake<IService>();
    
    testedObject.CallService("data");
    
    // verify your specific call to .PostData
    A.CallTo(() => service.PostData("data")).MustHaveHappened(Repeated.Exactly.Once);
    // verify that no more than 1 call was made to fake object
    A.CallTo(service).MustHaveHappened(Repeated.Exactly.Once); 
    

    The A.CallTo(object) overload allows you to make a generic setup/verification on all and any of the fake object methods.