English 中文(简体)
TaskSulleter.FromCurrentSynchronizationCon text - how to use WPF senter thread when unit test
原标题:TaskScheduler.FromCurrentSynchronizationContext - how to use WPF dispatcher thread when unit testing

I ve got code in a ViewModel that calls a service through a task. When the task finishes, it ll populate an ObservableCollection. The problem is that it s waiting on the task to finish by using the ContinueWith method and providing TaskScheduler.FromCurrentSynchronizationContext as task scheduler, so that the OC gets updated on the UI thread.

So far so good, but when it comes to unit testing, it throws an exception saying that "the current SynchronizationContext may not be used as a TaskScheduler." If I use a mock SynchronizationContext on the unit test, then the ObservableCollection throws an error because it s being updated based from the dispatcher thread.

Is there any way to work around this?

感谢。

最佳回答

它并非完全容易,而是实际上不是这样。 你们需要做的是作为STA组建的工人配对,以及你开始停电。 一旦工人到场,你就可以从显然没有开始从事此类工作的单位测试线向它派出工作。 因此,首先,在座各位如何开始在你的测试设置中做好派遣准备:

this.dispatcherThread = new Thread(() =>
{
   // This is here just to force the dispatcher infrastructure to be setup on this thread
   Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
   {
       Trace.WriteLine("Dispatcher worker thread started.");
   }));

   // Run the dispatcher so it starts processing the message loop
   Dispatcher.Run();
});

this.dispatcherThread.SetApartmentState(ApartmentState.STA);
this.dispatcherThread.IsBackground = true;
this.dispatcherThread.Start();

现在,如果你想在我建议你做的测试清理中彻底关闭,你就做以下工作:

Dispatcher.FromThread(this.dispatcherThread).InvokeShutdown();

因此,所有基础设施都偏离了道路,因此,在你检验如何在座标上执行时,你们都需要做。

public void MyTestMethod
{
    // Kick the test off on the dispatcher worker thread synchronously which will block until the work is competed
    Dispatcher.FromThread(this.dispatcherThread).Invoke(new Action(() =>
    {
        // FromCurrentSynchronizationContext will now resolve to the dispatcher thread here
    }));
}
问题回答

在您的初始测试中添加这一内容,以便形成一个背景:

SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

这使情况更加简单

http://www.un.org/Depts/DGACM/index_french.htm

其后,您的代码可明确使用<代码>scheduler(原文为)。 TaskSulstaler.FromCurrentSynchronizationContext(,期间)





相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签