在 NUnit 测试中使用 WPF 组件 - 如何使用 STA?
我需要在 NUnit 单元测试中使用一些 WPF 组件。我通过 ReSharper 运行测试,在使用 WPF 对象时失败并出现以下错误:
系统.InvalidOperationException:
调用线程必须是 STA,因为许多 UI 组件都需要它。
我已经读过有关此问题的信息,听起来该线程需要 STA,但我还没想出如何做到这一点。触发问题的是以下代码:
[Test]
public void MyTest()
{
var textBox = new TextBox();
textBox.Text = "Some text"; // <-- This causes the exception.
}
I need to use some WPF components in an NUnit unit test. I run the test through ReSharper, and it fails with the following error when using the WPF object:
System.InvalidOperationException:
The calling thread must be STA, because many UI components require this.
I've read about this problem, and it sounds like the thread needs to be STA, but I haven't figured out how to do this yet. What triggers the problem is the following code:
[Test]
public void MyTest()
{
var textBox = new TextBox();
textBox.Text = "Some text"; // <-- This causes the exception.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在更新的版本中,该属性已更改:
With more recent versions, the attribute has changed :
您应该将 RequiresSTA 属性添加到您的测试类中。
You should add the RequiresSTA attribut to your test class.
您尝试过这个吗?
Have you tried this?