[rspec-users] [Q] testing WIN32OLE_EVENT callbacks

Matt Wynne
Thu, 27 Aug 2009 15:12:47 -0000

On 26 Aug 2009, at 19:57, Chuck Remes wrote:

> I''m trying to setup some specs (really just assertions) that
verify
> some callbacks are executed in response to COM events. In the
> WIN32OLE_EVENT class you may subscribe to a COM event and have it
> delivered to you for processing. Syntax looks like:
>
>              event_handler.on_event(''StartEvent'') do
|*args|
>                do_start_event args
>              end
>
> The #do_start_event method is a member of the containing class. The
> only way I can confirm that it is being called is to set an
> expectation on the class under test. I''ve always noted that
mocking/
> stubbing the class under test is rather bad form. Regardless, I
> don''t know how to "mock" the delivery of an event anyway
so this may
> all be moot.
>
> I searched the list archives for ''win32ole'' back to early
2007 and
> came up with 0 hits. Any suggestions on how to tackle this?

This doesn''t really have much to do with the specific technology
you''re dealing with, it''s an issue about separating the layers
so you
can introduce fakes where you need to in order to test your own code.

Can you use dependency injection (or some other trick) to swap in a
fake event_handler into the code you''ve quoted above? If so, you can
then get your fake to simulate the behaviour of the event_handler and
exercise the code you want to actually test.

There are a couple of good books on these sort of techniques:
http://xunitpatterns.com/
http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052

does that help?

Matt