实例化视图不起作用

发布于 2024-11-09 11:54:00 字数 439 浏览 5 评论 0原文

嘿伙计们!
我们曾经使用 FlexUnit 编写单元测试,并且只是测试我们的模型。现在我们也想测试视图。在运行测试之前,我创建视图和模型的实例来测试这些内容。当我尝试访问视图时,出现空指针异常。如果我将视图添加到显示列表中,它会以某种方式起作用 - 即使我在添加后立即将其从列表中删除。

它看起来像这样:

var myView: MyView = new myView();
//myView.initialize(); will throw error
Application.application.addChild(view);
Application.application.removeChild(view);
myView.initialize(); // will work


希望你能给我一个提示。

西姆斯

Hey guys!
We used to write our UnitTests with FlexUnit and we were just testing our model. Now we want to test the view too. Before i run my tests i create an instance of my view and my model to test the stuff. When i try to access the view i get a null pointer exception. If i add the view to the displaylist it somehow works - even if i remove it from the list right after adding.

it looks something like this:

var myView: MyView = new myView();
//myView.initialize(); will throw error
Application.application.addChild(view);
Application.application.removeChild(view);
myView.initialize(); // will work

Hope you can give me a hint.

Sims

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分開簡單 2024-11-16 11:54:00

Flex UIComponent 在添加到容器之前不会经历组件生命周期。因此,如果您从未将其添加到容器中,则可能无法初始化变量,也可能无法创建子级。

有关 Flex 组件生命周期的详细信息。您会注意到将组件添加到容器后有 11 个步骤。

我怀疑添加它然后删除它可能会导致其他问题,但这取决于您要测试的内容。

要了解确切的错误,我们必须查看视图的初始化方法中包含哪些代码。它很可能访问尚未创建的子项。

MXML 组件通常会屏蔽生命周期步骤,但组件仍会执行这些步骤。

我希望这会有所帮助;但由于您没有提出问题,我不确定这是否是您想要的信息。

Flex UIComponents do not walk through the component lifecycle until after they are added to a container. As such, variables may not be initialized and children may not be created if you never add it to a container.

More info on the Flex Component LifeCycle. You'll notice there are 11 steps after the component is added to the container.

I suspect that adding it, then removing it, could cause other issues but it depends what you're trying to test.

To know your exact error, we'd have to see what code is in the initialize method on the view. Most likely it accesses a child that was not created yet.

MXML components will often masks the lifecycle steps, but a component will still go through them.

I hope this helps; but since you didn't ask a question I'm not sure if that was the information you were after.

烟酒忠诚 2024-11-16 11:54:00

除了我也正要发布的 (www.Flextras.com) 所写的内容之外,您可能会考虑采用不同的方法来测试您的观点。

首先,考虑分离模式,如表示模型、MVP 或 MVC。它们允许您将视图与视图行为分开,并测试与视图分开的行为。这样的方法如果正确完成,将会帮助您走很长的路,因为您可以最大限度地减少或消除您认为的 AS3 代码。

如果您确实想测试您的观点,我建议您使用功能测试工具。单元测试框架非常适合单独测试类。一旦您开始谈论具有复杂生命周期的视图,功能测试框架就开始有意义。

查看 FlexMonkey 作为 Flex 功能 UI 测试框架的示例。

In addition to what (www.Flextras.com) wrote, which I was just about to post as well, you might consider a different approach to testing your views.

First, consider a separation pattern like Presentation Model, MVP or MVC. They allow you to separate your view from your view behavior and test the behavior separate from the view. An approach like this, when done correctly, will take you a long way because you minimize or eliminate the AS3 code in your view.

If you really want to test your views, I would suggest that you use a functional testing tool for this. Unit test frameworks are good for testing classes in isolation. Once you start talking about views, which have complicated lifecycles, a functional testing framework starts to make sense.

Look at FlexMonkey as an example of a functional UI testing framework for Flex.

忆悲凉 2024-11-16 11:54:00

我建议您使用此处此处。此功能专为 UI 组件测试而设计。

I recommend you to use User Interface Facade described here or here. This functionality is designed specially for UI componets testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文