Xunit.StaFact | based tests on an STA thread
kandi X-RAY | Xunit.StaFact Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
Xunit.StaFact Key Features
Xunit.StaFact Examples and Code Snippets
[UIFact] // or [WinFormsFact] or [WpfFact] public async Task WpfFact_OnSTAThread() { Assert.Equal(ApartmentState.STA, Thread.CurrentThread.GetApartmentState()); int originalThread = Environment.CurrentManagedThreadId; await Task.Yield(); Assert.Equal(originalThread, Environment.CurrentManagedThreadId); // still there }
Trending Discussions on Xunit.StaFact
Trending Discussions on Xunit.StaFact
QUESTION
I have had success with some other unit tests around WPF Windows. This one has got me.
I have an xunit
test project in dotnet core 5
that has a Window. The test sets the content of a frame to a new page. However the assertion to check the content is not null will not work as the ViewNavigator content is null.
This is using Xunit.StaFact to allow running tests under STA thread.
Window
[UIFact]
public void Navigate_To_Pages()
{
FrameNavigationWindow window = new();
var monitor = new ManualResetEventSlim(false);
window.ViewNavigator.NavigationService.Navigated += (sender, args) => monitor.Set();
window.ViewNavigator.ContentRendered += (sender, args) => monitor.Set();
// set content here
window.ViewNavigator.Content = new PageNoVm1();
monitor.Wait(TimeSpan.FromSeconds(5));
// content always null
Assert.Equal(typeof(PageNoVm1), window.ViewNavigator.Content.GetType());
}
Just an empty page.
I can't spot anything obviously wrong.
** Updates **
I have tried using different flavours of window.Dispatcher.Invoke
to set the Content
:
var monitor = new ManualResetEventSlim(false);
window.ViewNavigator.NavigationService.Navigated += (sender, args) => monitor.Set();
window.ViewNavigator.ContentRendered += (sender, args) => monitor.Set();
var page = new PageNoVm1();
window.Dispatcher.Invoke(() => window.ViewNavigator.Content = page);
monitor.Wait(TimeSpan.FromSeconds(3));
ANSWER
Answered 2021-May-01 at 14:13Why are you synchronously blocking for 3 or 5 seconds? If WPF needs some time, you're not feeding it this way -- you're starving it.
Try changing the test method to be async, and then use await to give WPF time:
-monitor.Wait(TimeSpan.FromSeconds(5));
+await Task.Delay(5000);
I don't know if this will solve the problem, but it's a better way to pause execution in general.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Xunit.StaFact
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page