简体   繁体   中英

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.

您可以使用匿名方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM