Replies: 2 comments 7 replies
-
For now, I've settled on setting |
Beta Was this translation helpful? Give feedback.
2 replies
-
How about use the UITests framework? I open source the dotnetCampus.UITest.WPF And you can write the code that will run in the UI thread. [TestClass]
public class FooTest
{
[AssemblyInitialize]
public static void InitializeApplication(TestContext testContext)
{
UITestManager.InitializeApplication(() => new App());
}
[UIContractTestCase]
public void TestAsyncLoad()
{
"Waiting with async Loaded, then it do not lock UI Thread.".Test(async () =>
{
var mainWindow = new MainWindow();
var taskCompletionSource = new TaskCompletionSource();
mainWindow.Loaded += (sender, args) => taskCompletionSource.SetResult();
await mainWindow.Dispatcher.InvokeAsync(mainWindow.Show);
await taskCompletionSource.Task;
});
}
[UIContractTestCase]
public void TestMainWindow()
{
"Test Open MainWindow, MainWindow be opened".Test(() =>
{
Assert.AreEqual(Application.Current.Dispatcher, Dispatcher.CurrentDispatcher);
var mainWindow = new MainWindow();
bool isMainWindowLoaded = false;
mainWindow.Loaded += (sender, args) => isMainWindowLoaded = true;
mainWindow.Show();
Assert.AreEqual(true, isMainWindowLoaded);
});
"Test Close MainWindow, MainWindow be closed".Test(() =>
{
var window = Application.Current.MainWindow;
Assert.AreEqual(true, window is MainWindow);
bool isMainWindowClosed = false;
Assert.IsNotNull(window);
window.Closed += (sender, args) => isMainWindowClosed = true;
window.Close();
Assert.AreEqual(true, isMainWindowClosed);
});
}
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Data binding is easy to get wrong or regress over time because the XAML will compile without any build errors even though the property being bound to may not exist at runtime. I have unit tests that walk my view model through steps that if the window were visible would cause data binding failures to be reported (in the special debugger decoration on the title bar, the VS debug window, and (most importantly) in the
PresentationTraceSources.DataBindingSource
object. The point of the test is to catch data binding errors as they are introduced and block regressions.This technique works great when the window is visible. But when I never call
Window.Show()
in the unit test, the data binding does not occur and no errors are logged. Because this is a test that runs on dev machines (very frequently when Live Unit Testing is running), I can't have the window appearing and disappearing with each test.How can I force WPF to perform data binding as if the window were visible, but without making it visible?
Beta Was this translation helpful? Give feedback.
All reactions