Mocking IDictionary<integer,TObject>.TryGetValue

143 views
Skip to first unread message

László Bittermann

unread,
Dec 8, 2021, 7:03:40 AM12/8/21
to Spring4D
My class contains a field of type IDictionary<integer,TObject>. One of the unit tested method of this class calls  TryGetValue method of this field. I tried to mock this call by the following:

procedure TElementRegisterTest.registerElement_RegisteredElement;
var
  newElement : TMyElement;
  idNewElement : cardinal;
begin
  newElement := createMyElement( 1, 2.2, '3.3' );
  try
    fIDMapMock.Setup.Executes(

      function ( const args_ : TCallInfo ) : TValue
      begin
        result := TValue.From<boolean>( TRUE );
        args_[1] := TValue.From<integer>( 0 );
      end

    ).When.TryGetValue( Arg.IsEqual<TMyElement>( newElement ), Arg.IsAny<integer> );
    idNewElement := fElementRegister.registerElement( newElement );
    Assert.AreEqual( 0, idNewElement );
  finally
    FreeAndNIL( newElement );
  end;
end;

But the compiler stops by an error at ...when.TryGetValue():

E2033 Types of actual and formal var parameters must be identical

This parameter passed by the tested method. The variable unknown for the test. So I used the Arg. How can I mock this method call?
Reply all
Reply to author
Forward
0 new messages