我还没能为NUnit想出这个。有一个类似的问题,here提出了这个例外。答案解决了xUnit的使用问题,并且提问者报告说他让它适用于MSTest。我尝试在Dispatcher.CurrentDispatcher.InvokeShutdown();
,[TearDown]
和[TestFixtureTearDown]
方法中调用[Test]
,但仍然获得例外。
关于我的实现的一些细节:我创建了一个扩展System.Windows.Window的InputBox类。我创建了一个静态方法InputBox.Show(prompt)
,它执行以下代码:
var input = "";
var t = new Thread(() =>
{
var inputBox = new InputBox(prompt);
inputBox.ShowDialog();
input = inputBox.Input;
}) {IsBackground = true};
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return input;
有什么想法吗?
答案 0 :(得分:1)
感谢有关Dispatcher调用的评论中的想法。我改变了我的InputBox.Show方法,所以它现在看起来像这样,并且它工作得很好。我的单元测试中没有任何Dispatcher
次调用,我没有得到例外。
public static string Show(string prompt)
{
string input = null;
var t = new Thread(() =>
{
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
var inputBox = new InputBox(prompt);
inputBox.ShowDialog();
input = inputBox.Input;
}));
Dispatcher.CurrentDispatcher.InvokeShutdown();
}) { IsBackground = true };
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return input;
}
答案 1 :(得分:0)
您是否尝试过[RequireMTA](使用intelissence来证明正确性)属性? ApartmentState.STA - 在我看来是给您带来麻烦的陈述