The tests are pasing. The UI is working. The tests sometimes hang.
This commit is contained in:
@@ -9,9 +9,9 @@ namespace WpfViewModelFirst.Tests
|
||||
[Fact]
|
||||
public void Part1WillSetPart1ViewModelToCurrentViewModel()
|
||||
{
|
||||
Part1ViewModel part1ViewModel = new(() => { });
|
||||
Part1ViewModel part1ViewModel = new(() => { return Task.CompletedTask; });
|
||||
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
||||
.Returns(part1ViewModel);
|
||||
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||
main.Part1(null);
|
||||
@@ -22,19 +22,21 @@ namespace WpfViewModelFirst.Tests
|
||||
{
|
||||
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
|
||||
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
||||
|
||||
.Returns(() => new Part1ViewModel(() =>
|
||||
{
|
||||
//taskCompletionSource.SetResult(true);
|
||||
return Task.CompletedTask;
|
||||
|
||||
}))
|
||||
.Callback<Action>(c =>
|
||||
.Callback<Func<Task>>(c =>
|
||||
{
|
||||
//taskCompletionSource.SetResult(true);
|
||||
c.Invoke();
|
||||
})
|
||||
;
|
||||
Task.Run(() =>
|
||||
{
|
||||
c.Invoke();
|
||||
});
|
||||
});
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
||||
.Returns(() => new Part2ViewModel());
|
||||
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||
@@ -42,6 +44,10 @@ namespace WpfViewModelFirst.Tests
|
||||
main.Part1(null);
|
||||
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
||||
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
||||
taskCompletionSource = new();
|
||||
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
||||
Assert.Single(main.Strings);
|
||||
|
||||
}
|
||||
|
||||
Func<Task> Part1Func()
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace WpfViewModelFirst
|
||||
{
|
||||
public interface IViewModelFactory
|
||||
{
|
||||
Part1ViewModel GetPart1ViewModel(Action finishAction);
|
||||
Part1ViewModel GetPart1ViewModel(Func<Task> finishAction);
|
||||
Part2ViewModel GetPart2ViewModel();
|
||||
}
|
||||
}
|
||||
@@ -29,28 +29,32 @@ namespace WpfViewModelFirst
|
||||
CurrentViewModel = null;
|
||||
CurrentViewModel = _viewModelFactory.GetPart1ViewModel(Part1Action);
|
||||
}
|
||||
private void Part1Action()
|
||||
private Task Part1Action()
|
||||
{
|
||||
CurrentViewModel = null;
|
||||
var currentDispatcher = Dispatcher.CurrentDispatcher;
|
||||
LongDelay().ContinueWith((c) =>
|
||||
return LongDelay().ContinueWith((c) =>
|
||||
{
|
||||
//System.Diagnostics.Debug.WriteLine(c.Result);
|
||||
CurrentViewModel = _viewModelFactory.GetPart2ViewModel();
|
||||
ItHappened?.Invoke();
|
||||
currentDispatcher.Invoke(() =>
|
||||
ItHappened?.Invoke();
|
||||
currentDispatcher.Invoke(() =>
|
||||
{
|
||||
Strings.Add($"{DateTime.Now}");
|
||||
});
|
||||
ItHappened?.Invoke();
|
||||
}, TaskContinuationOptions.OnlyOnRanToCompletion);
|
||||
}
|
||||
|
||||
public event Action ItHappened;
|
||||
public event Action? ItHappened;
|
||||
|
||||
private async Task<string> LongDelay()
|
||||
{
|
||||
// This would be the Repository call in the production application
|
||||
await Task.Delay(500);
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(500);
|
||||
});
|
||||
return "Completed";
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
<RowDefinition/>
|
||||
</Grid.RowDefinitions>
|
||||
<Button Grid.Row="0"
|
||||
HorizontalAlignment="Center"
|
||||
VerticalAlignment="Center"
|
||||
Padding="10"
|
||||
Content="Finish"
|
||||
Command="{Binding FinishCommand}"/>
|
||||
</Grid>
|
||||
|
||||
@@ -9,9 +9,9 @@ namespace WpfViewModelFirst.Part1
|
||||
{
|
||||
public class Part1ViewModel : ViewModelBase
|
||||
{
|
||||
private readonly Action _finishAction;
|
||||
private readonly Func<Task> _finishAction;
|
||||
|
||||
public Part1ViewModel(Action finishAction)
|
||||
public Part1ViewModel(Func<Task> finishAction)
|
||||
{
|
||||
_finishAction = finishAction;
|
||||
FinishCommand = new CustomCommand(Finish);
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace WpfViewModelFirst
|
||||
{
|
||||
public class ViewModelFactory : IViewModelFactory
|
||||
{
|
||||
public Part1.Part1ViewModel GetPart1ViewModel(Action finishAction)
|
||||
public Part1.Part1ViewModel GetPart1ViewModel(Func<Task> finishAction)
|
||||
{
|
||||
return new(finishAction);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user