PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: pickettjd on September 11, 2008, 01:19:51

Title: Unit Testing Mediators
Post by: pickettjd on September 11, 2008, 01:19:51
Does anyone test mediators?  I've been running into a specific problem, that I hope you can help me with.

I have been trying to create a mock view component that I can use to send an event to the mediator that I'm testing.  In the TestCase, I register the mediator with the facade like so:

  var mockAddressForm:MockAddressForm = new MockAddressForm;
  facade.registerMediator(new AddressMediator(mockAddressForm));

I've been following the standard pattern of creating mediators that have a getter method that returns the view component.  So the mediator that I'm trying to test (AddressMediator) has the following constructor and getter method:

  public function AddressMediator( viewComponent:Object ) {
    super( NAME, viewComponent );
    // Add the events we want to hear from the viewComponent
    viewComponent.addEventListener( AddressForm.SAVE, onSave);
  // Cast the viewComponent as the appropriate mxml component so we can reference it.   
  private function get addressForm():AddressForm{
    return viewComponent as AddressForm;

The problem is that when the addressMediator makes reference to the viewComponent, it does so through the above getter.  The mock object that I have created is not the same class as the actual viewComponent.  So when this code in the mediator is called:

  private function onSave( event:Event ):void {
    sendNotification(ApplicationFacade.SAVE_STUFF, addressForm.stuff);

The addressForm getter returns null.  I'm assuming this is because of the mismatch in packages, which makes sense.  But that begs the question, what does everyone else do to test a mediator?

A whole other question is, is it worth the effort?

Thanks for your help!


Title: Re: Unit Testing Mediators
Post by: puremvc on September 11, 2008, 04:00:22
They are difficult to test. I suggest looking at the framework unit tests for mediators.