Nunit 3.12.0 在 WPF 视图模型单元测试中缺少 [RequiresSTA]。调用线程必须是STA

Nunit 3.12.0 missing [RequiresSTA] in WPF view model unit test. The calling thread must be STA

将 Nunit 从 v2.6.3 版本更新到 3.12.0 后。我的单位停止通过。 问题出现在单元测试中,RequiresSTAAttribute could not be found after update.

[Test]
[RequiresSTA]
public void When_Smart_Motion_Activity_Is_Selected_The_Values_Are_Valid()
{
     SomeViewModel vm = ViewModelHelper.CreateDefaultViewModel();
     ...
}

删除属性后测试失败,出现以下异常:

System.InvalidOperationException : The calling thread must be STA, because many UI components require this. at System.Windows.Input.InputManager..ctor() at System.Windows.Input.InputManager.GetCurrentInputManagerImpl() at System.Windows.Input.KeyboardNavigation..ctor() at System.Windows.FrameworkElement.FrameworkServices..ctor() at System.Windows.FrameworkElement.EnsureFrameworkServices() at System.Windows.FrameworkElement..ctor() at System.Windows.Controls.Panel..ctor() at System.Windows.Controls.Canvas..ctor() at project..... :line xyz

对于适用于 3.12.0 的 [RequiresSTA],是否有任何建议的解决方法或替代方法?

您可以使用:

[Test]
[Apartment(ApartmentState.STA)]
public void When_Smart_Motion_Activity_Is_Selected_The_Values_Are_Valid()
{
     SomeViewModel vm = ViewModelHelper.CreateDefaultViewModel();
     ...
}