Mock call to TProc

21 views
Skip to first unread message

Anders Gustavsson

unread,
Mar 18, 2025, 8:20:26 AMMar 18
to Spring4D
I want to mock an interface like this:

IMyInterface = interface
  procedure DoSomething(AProc: TProc);
end;

Trying something like this I get lost:

var mock: Mock<IMyInterface>;
mock.Setup.Execute(
  function(const ACallInfo: TCallInfo): TValue
  begin
    <???>
  end).When.DoSomething(<someFunction>);

Is there a way to set this up by replacing the angle enclosed items with something working?

Stefan Glienke

unread,
Mar 18, 2025, 8:23:29 AMMar 18
to Spring4D
ACallInfo[0].AsType<TProc> 

Anders Gustavsson

unread,
Mar 18, 2025, 8:43:58 AMMar 18
to spri...@googlegroups.com
I guess you mean inside the anonymous method? And at the end:

...When(TArgs.Any).DoSomething(nil);

?

--
You received this message because you are subscribed to the Google Groups "Spring4D" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring4d+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/spring4d/7e235d0a-53e8-4d03-a117-c3979fa0adb3n%40googlegroups.com.

Stefan Glienke

unread,
Mar 18, 2025, 8:53:40 AMMar 18
to Spring4D
I assume that you want to mock the behavior of calling any passed TProc:

  mock.Setup.Executes(
    function (const ACallInfo: TCallInfo): TValue
    begin
      ACallInfo[0].AsType<TProc>()();
    end).When.DoSomething(Arg.IsAny<TProc>());

Yes, all the parentheses are necessary because of the way the compiler deals with functions returning callable types.

Anders Gustavsson

unread,
Mar 18, 2025, 10:12:50 AMMar 18
to Spring4D
That was great! Thank you!
Reply all
Reply to author
Forward
0 new messages