Test using the Callback and DispatcherFrame
This commit is contained in:
@@ -42,6 +42,35 @@ namespace WpfViewModelFirst.Tests
|
|||||||
// Assert.Single(main.Strings);
|
// Assert.Single(main.Strings);
|
||||||
//}
|
//}
|
||||||
[Fact]
|
[Fact]
|
||||||
|
public async Task Part1CallBackExample()
|
||||||
|
{
|
||||||
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
||||||
|
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
||||||
|
.Returns(() => new Part2ViewModel());
|
||||||
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||||
|
TaskCompletionSource<bool> taskCompletionSource = new();
|
||||||
|
main.ItHappened += () => taskCompletionSource.SetResult(true);
|
||||||
|
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
||||||
|
.Callback<Func<Task>>(c =>
|
||||||
|
{
|
||||||
|
Task.Run(() =>
|
||||||
|
{
|
||||||
|
DispatcherFrame frame = new();
|
||||||
|
frame.Dispatcher.Invoke(
|
||||||
|
DispatcherPriority.Normal, () =>
|
||||||
|
{
|
||||||
|
c.Invoke();
|
||||||
|
frame.Continue = true;
|
||||||
|
});
|
||||||
|
Dispatcher.PushFrame(frame);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
main.Part1(null);
|
||||||
|
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
||||||
|
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
||||||
|
Assert.Single(main.Strings);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
public async Task Part1ActionWillSetCurrentViewModel()
|
public async Task Part1ActionWillSetCurrentViewModel()
|
||||||
{
|
{
|
||||||
Mock<IViewModelFactory> mockViewModelFactory = new();
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
||||||
|
|||||||
Reference in New Issue
Block a user