在 Flex 中设置功能测试
我正在为加载外部配置文件的应用程序设置功能测试套件。现在,我使用 flexunit 的 addAsync 函数来加载它,然后再次测试内容是否指向存在且可以访问的服务。
这样做的问题在于,拥有这种两阶段(或更多阶段)的方法意味着我在一个包含数十个断言的测试上下文中运行所有测试,这似乎是一种使用框架的退化方式,并使错误更难发现。有没有办法进行异步设置之类的东西?是否有另一个测试框架可以更好地处理这个问题?
I'm setting up a functional test suite for an application that loads an external configuration file. Right now, I'm using flexunit's addAsync function to load it and then again to test if the contents point to services that exist and can be accessed.
The trouble with this is that having this kind of two (or more) stage method means that I'm running all of my tests in the context of one test with dozens of asserts, which seems like a kind of degenerate way to use the framework, and makes bugs harder to find. Is there a way to have something like an asynchronous setup? Is there another testing framework that handles this better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很简单,但我花了两天时间才弄清楚。
解决方案:
首先您需要在某处创建一个静态变量。
有一个由flexunit框架创建的FlexUnitApplication.as,在onCreationComplete()函数中,您可以将stage设置为之前创建的静态引用:
当您在程序中访问该stage时,您应该将其替换为:
It is pretty easy, but took me 2 days to figure it out.
The solution:
First you need to create a static var somewhere.
There is a FlexUnitApplication.as created by the flexunit framework, and at the onCreationComplete() function, you can set the stage to the static reference created previously:
and when you would access the stage in the program, you should replace it to:
假设您使用的是 FlexUnit 4,可以从 [BeforeClass] 方法调用 addAsync:
话虽如此,测试访问文件的代码实际上是一个集成测试。我也很难反对使用 ASMock :)
Assuming you're using FlexUnit 4, addAsync can be called from a [BeforeClass] method:
Having said that, testing code that accesses a file is really an integration test. I'm also hardly in a position to argue against using ASMock :)
听起来您需要删除加载该外部文件的依赖关系。几乎所有异步测试都可以通过使用模拟框架来删除。 ASMock 是 Flex 的绝佳选择。它将允许您伪造 URLLoader 对象并返回伪造的配置来运行测试。模拟将帮助您编写更好的单元测试,因为您可以同步或异步模拟所有依赖项。
Sounds like you need to remove the dependency of loading that external file. Pretty much all Aysnchronous tests can be removed through the use of a mocking frameworks. ASMock is an awesome choice for Flex. It will allow you to fake the URLoader object and return faked configurations to run your tests against. Mocking will help with you write much better unit tests as you can mock all dependencies synchronous or asynchronous.