Background
I have a ViewModel that I want to unit test using the built-in test framework from Visual Studio.
public async Task RefreshEntries(string rootID)
{
_isCurrentlyFetchingEntries = true;
if (_entriesCollectionSource == null)
_entriesCollectionSource = new ObservableCollection<ExplorerDisplayEntryDTO>();
_entriesCollectionSource.Clear();
Entries = CollectionViewSource.GetDefaultView(_entriesCollectionSource);
var entries = await Task.Run(() =>
{
var toReturn = (...) // Fetch plenty of things in my repo
return toReturn;
});
foreach (var entry in entries)
{
_entriesCollectionSource.Add(entry);
}
Entries.Filter = _customizedFilter;
_isCurrentlyFetchingEntries = false;
}
I wrote a unit test that will indirectly await this specific task (through multiple calls all around the ViewModel).
When running the unit test, (CTRL+R, T) it passes with no issue.
When debugging the unit test, an exception is thrown at line 16 of the snippet above
When running the program normally (both in debug and release mode), the method does not trigger any error.
System.NotSupportedException This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread
I took a look at the parallel stacks window and it seems that when running normally (and I suspect, when running the unit test), the line is executed by the main thread. Whereas it seems to be executed by some other thread while debugging the test.
Question
What sort of behavior changes should I expect when running a unit test vs debugging a unit test?