views:

1137

answers:

3

I'm trying to use callLater with FlexUnit v0.9:

public function testCallLater():void {
   Application.application.callLater( addAsync(function():void {
      assertTrue(true);
   }, 1000));
}

but when it runs I get this error:

ArgumentError: Error #1063: Argument count mismatch on flexunit.framework::AsyncTestHelper/handleEvent(). Expected 1, got 0.
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628]
at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]

I'm not sure what the problem is. Is callLater incompatible with FlexUnit?

+1  A: 

It looks like you are expecting an event, but not getting one. I imagine the following code would work.

public function testCallLater():void {
   Application.application.callLater( addAsync(function(/*removed event declaration*/):void {
      assertTrue(true);
   }, 1000));
}
t3hh00d
That doesn't work. It gives the same error.
paleozogt
+3  A: 

First, you should really consider migrating to FlexUnit 4.0: http://blogs.digitalprimates.net/codeSlinger/index.cfm/2009/5/3/FlexUnit-4-in-360-seconds

Second, callLater is meant to be used to delay processing until the next frame in visual classes. Your test case class is not a visual class extending UIComponent, therefore you should not try to use callLater.

Third, addAsync is use to test the results of an asynchronous operation. This is typically used in testing the results of a network request, of a file read, of a timer event, etc. That is why normally you see an "event" as a parameter in the addAsync test function (because asynchronous requests use events to process results). In your case, you're not responding to an asynchronous operation with your addAsync call, and therefore you shouldn't be looking for an event in your test function. Remove the event:Event parameter and the error will go away.

However, perhaps you can re-phrase this question to state what you're trying to accomplish? The code sample that you've indicated is not really doing anything useful. If you can be a little more specific we can help you write a better test case.

For help with using addAsync with older versions of FlexUnit, see this tutorial: http://life.neophi.com/danielr/2007/03/asynchronous_testing_with_flex.html

darronschall
First, FlexUnit is still in beta with no ant support yet, so I'm stuck with FU1. Second, I removed the question's context so as to keep the question easy to understand. Third, removing the event param doesn't change the runtime error.
paleozogt
The beta of FlexUnit 4 is just as stable, if not more so, than the original FlexUnit code. Don't let the "still in beta" fool you. The original FlexUnit isn't even 1.0 anyway.The way in which you're using addAsync is not compatible with callLater. As I've explained, addAsync is meant to handle events that arise from the result of asynchronous operations. Generally, you do not want to be using callLater inside of your test cases.What are you trying to test? There is most likely a better way to be writing the test case. Let us try and help you with that.
darronschall
Its not that FlexUnit4 isn't stable, its that there's no Ant support. If I can't use it in my CI tool, its a deal-breaker. I'm testing view classes and I want to set the currentState "later" after some tests/actions have been performed. callLater seemed to be the best way to go about it.
paleozogt
You use FlexUnit 4 with ANT the same way you use the current FlexUnit. Compile the test runner .mxml and grab the results. In fact, FlexUnit 4.0 has built-in test runners to help with CruiseControl and CI integration.What are you testing in the view? View code is notoriously hard to test. By using the presentation model pattern, you can separate the view from it's logic and test the logic independently of the view itself. Since the view relies on the presenter for everything, testing the presenter will give you "good enough" test coverage from a pragmatic standpoint.
darronschall
Anyway, back to the original question, try explicitly sending a null argument for the event as part of addAsync. The error message indicates that internally the async helper is expecting an argument but none was provided, so let's just pass through null:Application.application.callLater( addAsync(function():void { assertTrue(true); }, 1000), [null] );
darronschall
Perhaps you know something that I don't, but with FlexUnit v0.9 you can't just "compile the test runner .mxml and grab the results" in Ant. You have to use Peter Martin's FlexAntTask.jar/JUnitTestRunner stuff. Take a look at the comments on this page (http://blogs.digitalprimates.net/codeSlinger/index.cfm/2009/6/3/FlexUnit-4-and-Flash-Builder-4) where they say that CI integration for FlexUnit 4 isn't done yet. Am I missing something?
paleozogt
Using Application.application.callLater( addAsync(function():void { assertTrue(true); }, 1000), [null] );worked. Thanks.
paleozogt
A: 

Just in case someone needs it, this works :

private function testCallLater():void {
    Application.application.callLater(doCallLater, [ addAsync(funcUnderTest, 1000) ]);
}

private function doCallLater(testFunc:Function):void {
    testFunc(null);  // Dummy arg necessary because of addAsync expecting one arg
}

private function funcUnderTest(e:Object = null):void {
    assertTrue(true);
}
wdrai