I have encoutered problems when using NUnit to test a ViewModel which has ThreadSafeObservableCollection. The thread safety comes about through using the Dispatcher and when you call Dispatcher.BeginInvoke, you are instructing the dispatcher to run the delegates on its thread when the thread is idle.
When running unit tests, the main thread will never be idle. It will run all of the tests then terminate.
To overcome this I have written a trap which looks for the NUnit Thread Name. A true result and the ThreadSafeCollection proceeds without using the Dispatcher.
private bool CheckDispatcherAccess()
{
if (_dispatcher.Thread.Name == "TestRunnerThread")
{
return true;
}
return _dispatcher.CheckAccess();
}
...
Other references to the Dispatcher issue when using NUnit
Using the WPF Dispatcher in NUnit Tests
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment