views:

322

answers:

1

What methods do you use to unit test event handlers, particularly if they require information from the event (such as mouse coordinates, or the target of the event)? Is the most common practice to just refactor the behavior into a method that does the lifting while the handler just extracts information from the event, or are there effective ways to simulate event dispatch in FlexUnit or Fluint?

+2  A: 

Most often I create an event object manually and dispatch the event from the object which is being listened to, especially so I can make sure there's not floating event listeners which aren't getting removed properly. Occasionally I run across a situation in which my test classes can't get access to the dispatching object. If that's the case, I separate out the business logic from the actual event listener method, keep the event listener method extremely simple and lightweight, and test the business logic. Dispatching from the object is much better though, if you can.

FlexUnit and Flunit don't provide any "simulation" of event dispatching; if you can access the object, you'll be able to dispatch from it, and if you can't, FlexUnit/Flunit couldn't either.

When using events, though, you'll want to understand how FlexUnit/Flunit provide ways of doing async unit testing. Dispatching a mock event means you're starting an action which does not finish when the method is finished, so you'll have to create an asynchronous test. The documentation for FlexUnit is in their ASDocs, and Flunit has a good online doc set at their home site: http://code.google.com/p/fluint/wiki/AsyncTest

stevko