Copy link to clipboard
Copied
Hello,
I would like to now how to proceed to test a delegate class that is taking an IResponder as method parameter.
For exemple :
LoginDelegate.as (simplified version) :
public function login( responder : IResponder, username : String, password : String ) : void
{
var s : SomeService = new SomeService();
s.addEventListener( ResultEvent.RESULT, responder.result );
s.addEventListener( FaultEvent.FAULT, responder.fault );
s.login( username, password );
}
As you can see, the delegate communicate with some backend service and then calls the provided responder methods.
I tried to use Async.asyncResponder without luck:
[Before(async)]
public function runBeforeAsyncTest():void
{
var d : LoginDelegate = new LoginDelegate();
var responder : Responder = new Responder(result,fault);
Async.asyncResponder(this, responder, 5000);
d.login(responder, "someuser", "somepass");
}
public function result(data:Object):void
{
trace("result");
}
public function fault(info:Object):void
{
trace("fault");
}
[Test]
public function testLoginFail():void
{
trace("testLoginFail")
Assert.assertEquals("5", 5);
}
It goes in result method, then It fails with :
testLoginFail Error: Timeout Occurred before expected event
Can someone please tell me how to test my delegate properly
Thank you in advance
Copy link to clipboard
Copied
Ok this seems to work, but I don't know if it is considered best practice :
(I would still like to hear from someone used to test delegate)
[Before(async)]
public function runBeforeAsyncTest():void
{
delegate = new LoginDelegate();
user = null;
}
[After(async)]
public function runAfterAsyncTest():void
{
delegate = null;
}
[Test(async)]
public function testLoginPass():void
{
var responder : IResponder = Async.asyncResponder(this, new Responder(result,fault), 5000);
delegate.login(responder, VALID_USER_ID, VALID_USER_PASS);
}
public function result(data:Object):void
{
user = data as UserDTO;
Assert.assertTrue( user is UserDTO && user.id.toString() == VALID_USER_ID );
}
public function fault(info:Object):void
{
Assert.assertNull(user);
}
Copy link to clipboard
Copied
Take a look at the wiki pages for the Fluint project ( which is an ancestor of FlexUnit 4 ). This partioular page is about Cairngorm, but about 2/3 of the way through the page it discussed testing delegates.
Although the syntax will be slightly different now, I think the concepts are exactly what you are asking about:
http://code.google.com/p/fluint/wiki/Cairngorm
Mike
Copy link to clipboard
Copied
Hi Adnan,
whilst your test might be good it is not really a unit test - If it actually connects to a server of some kind than it is an integration test.
Normally when you want to unit test something that uses an external resource you 'fake' it. Depending on the kind of fake that you make it is either called a mock, stub or test double.
I am not conversant with cairngorn at all and so I can't comment on which bits would be best to fake, but I can point you at a very good framework for mocking that integrates nicely with flex unit 4 - Asmock. You can find it here
http://asmock.sourceforge.net/
Hope this helps
Conrad