FlexUnit 4.1
FlashBuilder 4.5.1
BACKGROUND
I'm trying to test a custom Flex 4 skinnable component, using the FlexUnit UIImpersonator class. If I run my tests from a FlashBuilder Spark only project everything works fine. If I try to test from a project with the mx component set on the classpath I get a "getElementIndex not available in non Flex 4 projects" error.
QUESTION
Can I unit test spark components in FlexUnits visual test environment while still having the mx component set on the classpath?
RESEARCH
UIImpersonator delegates it's method calls to a "testEnvironment". The implementation used for this "testEnvironment" is decided by the VisualTestEnvironmentBuilder class and the FlexEnvironmentBuilder class. If the FlexEnvironmentBuilder class can find the "mx.core.Container" on the classpath it returns a MX environment, else a Spark environment. Only the spark environment has valid implemen开发者_如何学Ctations for Flex 4 relevant method calls on the UIImpersonator – like the addElement method.
I have the same problem, and didn't found any solution. I suggest to file a bug, but since FlexUnit is in the process of being adopted by Apache Flex, I don't think it would be resolved anytime soon.
As a workaround. Just use UIImpersonator.addChild() and add a reference to mx.core.Container in your TestRunner. This ensures that a MX container is used as testEnvironment and you won't receive a 'getElementIndex...' error.
import mx.core.Container
public function runTests():void {
// reference to mx container
var containerRef:Container = new Container();
// Run your testsuite as usual. eg:
var core:FlexUnitCore = new FlexUnitCore();
core.run(MyTestSuite);
}
精彩评论