我需要在 NUnit 单元测试中使用一些 WPF 组件。我通过 ReSharper 运行测试,但在使用 WPF 对象时失败并出现以下错误:
System.InvalidOperationException:
The calling thread must be STA, because many UI components require this.
我读过这个问题,听起来线程需要是 STA ,但我还没有想出如何做到这一点。触发问题的是以下代码:
[Test]
public void MyTest()
{
var textBox = new TextBox();
textBox.Text = "Some text"; // <-- This causes the exception.
}
最佳答案
您应该添加 RequiresSTA属性到您的测试类。
[TestFixture, RequiresSTA]
public class MyTestClass
{
}
关于c# - 在 NUnit 测试中使用 WPF 组件 - 如何使用 STA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2220418/