Silverlight 单元测试,处理嵌套方法中的异步调用
当为 silverlight 创建单元测试时,测试框架似乎内置了一些功能来帮助异步调用,但仅限于测试方法内。如果在您正在调用的方法的子方法中存在异步调用怎么办?
作为示例,我们使用 Specflow 来测试我们的 ViewModel,我们想要调用命令的 Execute 方法,然后该命令异步调用 Web 服务。在断言结果之前如何确保 while 命令已完成?
作为示例,这是
[Asynchronous]
[When(@"I press calculate")]
public void WhenIPressCalculate()
{
_helloWorldViewModel.CalculateCommand.Execute(null);
}
我需要等待整个命令完成才能继续的命令。
When creating Unit Tests for silverlight the test framework seems to have features build in to help with Async calls but only within the test method. What if in a child method of the one you are calling there is an Async call.
As an example we use Specflow to test our ViewModels, We want to call the Execute method of a command and that command then asynchronously calls a web service. How can i ensure the while command is completed before asserting the results?
as an example this is the command
[Asynchronous]
[When(@"I press calculate")]
public void WhenIPressCalculate()
{
_helloWorldViewModel.CalculateCommand.Execute(null);
}
I need to wait for the entire command to complete before continuing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在同一方法中创建一个循环,并设置执行已完成的退出条件。
make a loop in the same method with an exit condition that execution has finished.
调用网络服务时循环不起作用,因为回调被编组到 UI 线程,如果循环某个条件,则会在回调等待线程完成时创建死锁。
不管怎样,我们解决了这个问题。事实证明,specflow 人员正在同时研究这个问题,并且正在制定解决方案。我们让它在预发布版本中运行。
我在这里写了一篇关于它的博客文章 http: //rburnham.wordpress.com/2011/05/13/testing-silverlight-asynchronous-code-with-specflow/
这可能会改变,因为这是通过预发布版本完成的,希望如此如果有的话我会找时间更新的。
A loop does not work when making a call to a network service because the callback is marshalled to the UI Thread, if it is looping for a condition it creates a deadlock as the call back is waiting for the thread to finish.
Anyway we soted this problem out. turns out the specflow guys were working on this at the same time and have a solution in progress. We got it working with a pre release build.
I wrote a blog entry about it here http://rburnham.wordpress.com/2011/05/13/testing-silverlight-asynchronous-code-with-specflow/
this may change as this was done with a pre release build, hopefully I'll get time to update it if it does.