onrails.org home

Sweet way to write Flex Unit tests for Rails

Using ActiveResources from Flex? Using FlexUnit? Here is a nice way to write your tests.

package tests
import flexunit.framework.*;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
import resources.Raffles;

public class TestRaffles extends BaseTestCase
private var raffles:Raffles;
public function TestRaffles(name : String = null)
raffles = new Raffles();
public function testRemoteFindRaffle():void
public function assertRemote_testRemoteFindRaffle(data:Object):void
Assert.assertTrue(“Raffle show successfully called”, data is ResultEvent);
assertEquals(“MyString”, data.result.name);


Note this code is not yet a plugin and is using code you can find here: http://code.google.com/p/flexonrails/source. I was starting to use it on multiple projects so I thought it was to time find a home for it. Also it is using the org.onrails.rails.ActiveResourceClient Flex class. I would recommend that you use Alex MacCaw’s ActvieResrouce for Actionscript. I still need to talk with Alex and integrate this fixture loading code with his code.

The BaseTestCase Flex class is an extended TestCase that provides support for fixtures. Now in your constructor you can define which fixtures you want to reload between each test. Only tests methods starting with “testRemote” will trigger refreshing the fixtures. As you know, when using AMF or HttpService remote invocations are asynchronous and you cannot test the result of a remote call in the same method than where the call is made from. That’s why I added the assertRemote method which takes an AsyncToken as parameters. This will automatically invoke a method whos name starts with assertRemote_ followed by the test method name. This simplifies greatly writing asynchronous tests. FlexUnit provides the addAsync method, we just add the convenience assertRemote function to setup all the callbacks.

To make this work for you Flex with Rails project. You need to fixtures_controller.rb to your controllers and setup the following routes:

if RAILS_ENV == “test” map.resources :fixtures, :new => { :test_results => :post } map.crossdomain ‘/crossdomain.xml’, :controller => ‘fixtures’, :action => ‘crossdomain’ end

You need to extend your Flex TestCase from tests.BaseTestCase.


Fork me on GitHub