c# - 在 NUnit 测试中使用 WPF 组件 - 如何使用 STA?

标签 c# wpf unit-testing nunit resharper

我需要在 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/

相关文章:

c# - 将鼠标指针移动到选项卡按钮将您带到的位置 C#

c# - 在 XAML-WPF 中设置列​​标题名称

Python unittest mock ...模拟一个模块语句

unit-testing - Grails Spock 单元测试 - 什么是 "1 *"以及为什么是 "too few invocations"?

c# - 什么 block 没有被覆盖?

c# - 我坚持的正则表达式问题

c# - ASP.NET MVC - 验证逻辑 - 在哪里放置?

c# - 重命名命名空间后无效的 .resx 文件

c# - 以编程方式将 StackPanel 添加到 ComboBoxItem

wpf - 我可以在 VS2008 中开发也可以在 Mac 上运行的桌面应用程序吗?什么方法?