-
Notifications
You must be signed in to change notification settings - Fork 731
Closed
Description
Please consider the following test:
[TestMethod]
public async Task Test()
{
var sut = new ViewModel();
sut.MonitorEvents();
await sut.DoSomethingAsync();
sut.ShouldRaisePropertyChangeFor(vm => vm.BusyErrorText);
}Sometimes, this test fails by an InvalidOperationException with description "Object <{0}> is not being monitored for events or has already been garbage collected. Use the MonitorEvents() extension method to start monitoring events."
The reason is the ThreadStatic-Attribute which is used for decorating the EventRecordersMap in the EventMonitor class.
Typically, UnitTests does not have a synchronization context so the code after await will be executed on any idle thread which is not the same thread as the thread that has been started the test. As a result, a new (empty) EventRecordersMap will be created and used when calling sut.ShouldRaisePropertyChangeFor(...).
bitbonk, borismod, sivukhin and b099l3