delphidelphi-mocks

TList<T>.count return value mocking with Delphi Mocks


In my unit test there is a need to setup a mocked TList<T>. How can I setup a return value for a mocked TList<T>.count property read?

When I use count after When the compiler error message is:

[dcc32 Error] Unit1.pas(40): E2014 Statement expected, but expression of type 'Integer' found

When I use getCount after When the compiler error message is:

[dcc32 Error] Unit1.pas(40): E2003 Undeclared identifier: 'getCount'

The count property reads directly the fCount attribute. Is there any solution?

type
  TMyClass = class
  end;

procedure TXXXTestCase.testYYY;
var
  mL : TMock<TList<TMyClass>>;
begin
  mL := TMock<TList<TMyClass>>.create;
  try
    // ...
    mL.Setup.WillReturn( 1 ).When.Count;
    // ...
  finally
    mL.Free;
  end;
end;

Solution

  • OK. Inspired by Stefan Glienke comment I created a utility calss to avoid accessing a a porperty of a property of ... of a mocked object. I just pass the mock to the mock of the utility class to get the (fake) value.

    A code snipet looks like my original one:

    value = object1.property.list.count;
    

    The solution for this deep inside look:

    value = object1Utility.getListCount( object );
    

    The TObjec1tUtility calls Object2Utility to its respond, but in this case I can mock Object1Utility, no need to add a mock into a mock.