delphimockingspring4ddunitx

How to test the right way the call number of a mocked method in Spring4D 1.2.2?


My test just has to check the call number of a given mocked method, nothing else.

The tested class and the embedded interface:

type
  IMyInterface = interface ( IInvokable )
    ['{815BD1B0-77CB-435F-B4F3-9936001BA166}']
    procedure bar;
  end;

  TMyClass = class
    private
      fMyInterface : IMyInterface;

    public
      procedure foo;
  end;

procedure TMyClass.foo;
begin
  if ( someCondition ) then
    fMyInterface.bar;
end;

The test case:

procedure TMyClassUnitTest.foo_bar_NotCalled;
begin
  fMyTestedObject.foo;
  fMyInterfaceMock.Received( 0 ).bar;
end;

The runner says for foo_bar_NotCalled:

No assertions were made during the test!

What else should I do? What method of the Assert should I call?


Solution

  • DUnitX tracks if any call to Assert was made. In certain cases you just want to check if the code just ran properly without some explicit checks.

    In these cases you need to call Assert.Pass(); to satisfy the framework.