Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Unit Testing Mediators  (Read 3095 times)
pickettjd
Newbie
*
Posts: 1


View Profile Email
« 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!

jdp
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: September 11, 2008, 04:00:22 »

They are difficult to test. I suggest looking at the framework unit tests for mediators.

-=Cliff>
Logged
Pages: [1]
Print