Search code examples
delphidelphi-mocks

Delphi Mocks - Verify an overloaded method is never called


As the title suggests, I'm trying to write a test to verify that one version of a method is called, and that the overloaded version is not. Since Delphi-Mocks seems to use indexing on the parameter matching, I'm seeing a fail, and that the the overloaded function is being called when it is in fact, not.

Sample Test Interface

TFoo = class(TObject)
public 
    function Bar(const a, b, c: string) : string; overload;virtual;
    function Bar(const a: string) : string; overload;virtual;
end;

Sample Test Code

procedure TestClass.Test
var mock : TMock<TFoo>;
    bar : TBar;
begin
    mock := TMock<TFoo>.Create;
    bar := TBar.Create(mock);
    mock.Setup.Expect.Once.When.Bar('1','2','3');
    mock.Setup.Expect.Never.When.Bar(It(0).IsAny<string>());        

    //Will Wind up down an if-branch calling either bar(1) or bar(3)
    bar.Execute; 

    mock.VerifyAll;        
end;

Thanks!


Solution

  • You can use "WillExecute" to check this. For example:

    procedure TestClass.Test;
    var
      mock : TMock<TFoo>;
      bar : TBar;
      CheckPassed: Boolean;
    begin
      mock := TMock<TFoo>.Create;
      bar := TBar.Create(mock);
    
      CheckPassed := True;
      mock.Setup.WillExecute('Bar',
        function(const Args: TArray<TValue>; const ReturnType: TRttiType): TValue
        begin
          if Length(Args) = 2 then // one is for "Self"
            CheckPassed := False;
        end);
    
      //Will Wind up down an if-branch calling either bar(1) or bar(3)
      bar.Execute;
    
      Assert(CheckPassed);
    end;