Search code examples
delphiverifydelphi-mocks

How to verify multiple mock expectations with Delphi Mocks?


What is the difference between mock.verify and mock.verifyAll in Delphi Mocks? Does it verify expectations of other mocks as well? I want to verify all expectations of all my mocks created for the current unit test.


Solution

  • You can tell a mock of an interface that it also can mock other interfaces. This is useful if the interface you mock is asked via Supports for another interface.

    Verify checks if the expectations of the directly mocked type while VerifyAll also checks the expectations of the other interfaces.

    Example

    var
      foo: TMock<IFoo>;
    begin
      foo := TMock<IFoo>.Create;
      foo.Implements<IBar>;
      foo.Setup.Expect.Once.When.DoThis;
      foo.Setup<IBar>.Expect.Once.When.DoThat;
    
      // pass mock to tested component which 
      // calls DoThis and Supports(IBar) and calls DoThat
    
      foo.Verify; // checks if DoThis was called once
      foo.VerifyAll; // also checks if DoThat on the mock as IBar was called.
    end;