Skip to content

Not able to define behaviour for overloaded functions with different parameters. #141

@AndreasInpact

Description

@AndreasInpact

We recently upated the version of Delphi-Mocks we where using and with this version it looks like we can not define differen behaviour for overloaded functions.

  IMockIntf = interface(IInvokable)
    ['{E473945B-D350-4535-A981-B66A5B58DA21}']
    function Test(const Arg1: string): TArray<string>; overload;
    function Test(const Arg1, Arg2: string): TArray<string>; overload;
    function Test(const Arg1, Arg2, Arg3: string): TArray<string>; overload;
  end;

Given this interface it was possible (and needed) to define a return value for alle 3 functions separatly.

  FMock := TMock<IMockIntf>.Create;

  FMock.Setup.WillReturn('Test1').When.Test(It0.IsAny<string>);
  FMock.Setup.WillReturn('Test2').When.Test(It0.IsAny<string>, It1.IsAny<string>);
  FMock.Setup.WillReturn('Test3').When.Test(It0.IsAny<string>, It1.IsAny<string>, It3.IsAny<string>);

And than test the behaviour

procedure TMyTestObject.Test1;
begin
  var x := FMock.Instance.Test('A');
  Assert.AreEqual('Test1', x);
end;

procedure TMyTestObject.Test2;
begin
  var x := FMock.Instance.Test('A', 'B');
  Assert.AreEqual('Test2', x);
end;

procedure TMyTestObject.Test3;
begin
  var x := FMock.Instance.Test('A', 'B', 'C');
  Assert.AreEqual('Test3', x);
end;

Unfortenatly with the latest version this is not working anymore. I looks like the Mock sees the Test functions as one and will not allow this:

EMockSetupException with message '[IMockIntf] already defines Will Return When for method [Test]'

If I use AllowRedefineBehaviorDefinitions := True I can define the behaviour, but than the function with 2 parameters returns the behaviour with 3 parameter 'Test3' and the function with 3 parameter raises an Access violation at address 00000000.

I used the latest version in combination with Delphi 11.3.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions