Search code examples
delphiinterfacedelegatesdelphi-mocks

How can I Delphi Mocks an event handler or, assign interface method as delegate in Delphi?


So, I have a Delphi object with an event, which is a "reference to procedure()". Normally I can do this to assign the delegate.

MyObj.OnBooom := HandleOnBooom;

Now I want to unit test whether the event is called. So with Delphi Mocks, which relies on interface, I will make an interface that has a procedure "OnBooom", pass the interface to the TMock object. Then do this.

MyObj.OnBooom := FMockCaller.Instance.OnBooom;

But this won't compile because apparently Delphi thinks that, in the right side I am just calling the procedure of the interface, instead of wanting to assign it to the left side.

So, is there a way I can get this assign to work? or Is there other way to Delphi Mocks the event? Thanks.


Solution

  • You can use an anonymous method:

    MyObj.OnBooom := procedure begin FMockCaller.Instance.OnBooom; end;