Skip to content

Event monitoring is not working properly for asyncronous tests #477

@MichaWresche

Description

@MichaWresche

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(...).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions