SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.22k stars 751 forks source link

Dispatcher thread in MVVM application is blocking the tests. #2736

Open chandu07 opened 5 months ago

chandu07 commented 5 months ago

SpecFlow Version

3.9.22

Which test runner are you using?

xUnit

Test Runner Version Number

2.4.2

.NET Implementation

.NET 6.0

Project Format of the SpecFlow project

Sdk-style project format

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

No response

Issue Description

I have na MVVM application that has various ViewModels for different aspects of the project. In the MainWindowViewModel, I have a ProjectViewModel that will need to be instantiated based on the project that is open. As the projects are heavy and needs deserialization to load everytime, I am running the OpenProject method as an async task.

`Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

public async Task OpenProject() { dataCard = await Task.Factory.StartNew(() => _pManager.Deserialize()); _dispatcher.Invoke((Action)delegate { _pManager.BeginNewScope(dataCard); }); }`

This async OpenProject task has a part where we start the scope of the project in the main UI thread (_dispatcher) instead of the background task thread. Everything works fine when I am running the application. However, when running the OpenProject method with Specflow for acceptance testing, it goes into a loop after _dispatcher.Invoke. Could you please suggest what could be going wrong here?

Steps to Reproduce

Create an async task and run a dispatcher.invoke within it, like below.

'Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

public async Task OpenProject() { dataCard = await Task.Factory.StartNew(() => some_method); _dispatcher.Invoke((Action)delegate { some_command; }); }'

Link to Repro Project

No response