Working with passing tests!
This commit is contained in:
@@ -9,9 +9,9 @@ namespace WpfViewModelFirst.Tests
|
||||
[Fact]
|
||||
public void Part1WillSetPart1ViewModelToCurrentViewModel()
|
||||
{
|
||||
Part1ViewModel part1ViewModel = new(() => Task.CompletedTask);
|
||||
Part1ViewModel part1ViewModel = new(() => { });
|
||||
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||
.Returns(part1ViewModel);
|
||||
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||
main.Part1(null);
|
||||
@@ -22,24 +22,23 @@ namespace WpfViewModelFirst.Tests
|
||||
{
|
||||
TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
|
||||
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||
|
||||
.Returns(() => new Part1ViewModel(() =>
|
||||
{
|
||||
taskCompletionSource.SetResult(true);
|
||||
return Task.CompletedTask;
|
||||
//taskCompletionSource.SetResult(true);
|
||||
|
||||
}))
|
||||
.Callback<Func<Task>>(c =>
|
||||
.Callback<Action>(c =>
|
||||
{
|
||||
c.Invoke().ContinueWith(t =>
|
||||
{
|
||||
taskCompletionSource.SetResult(true);
|
||||
});
|
||||
//taskCompletionSource.SetResult(true);
|
||||
c.Invoke();
|
||||
})
|
||||
;
|
||||
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
||||
.Returns(() => new Part2ViewModel());
|
||||
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||
main.ItHappened += () => taskCompletionSource.SetResult(true);
|
||||
main.Part1(null);
|
||||
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
||||
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace WpfViewModelFirst
|
||||
{
|
||||
public interface IViewModelFactory
|
||||
{
|
||||
Part1ViewModel GetPart1ViewModel(Func<Task> finishAction);
|
||||
Part1ViewModel GetPart1ViewModel(Action finishAction);
|
||||
Part2ViewModel GetPart2ViewModel();
|
||||
}
|
||||
}
|
||||
@@ -27,23 +27,26 @@ namespace WpfViewModelFirst
|
||||
public void Part1(object? e)
|
||||
{
|
||||
CurrentViewModel = null;
|
||||
CurrentViewModel = _viewModelFactory.GetPart1ViewModel(async () => await Part1Action());
|
||||
CurrentViewModel = _viewModelFactory.GetPart1ViewModel(Part1Action);
|
||||
}
|
||||
private Task Part1Action()
|
||||
private void Part1Action()
|
||||
{
|
||||
var currentDispatcher = Dispatcher.CurrentDispatcher;
|
||||
CurrentViewModel = null;
|
||||
return LongDelay().ContinueWith((c) =>
|
||||
var currentDispatcher = Dispatcher.CurrentDispatcher;
|
||||
LongDelay().ContinueWith((c) =>
|
||||
{
|
||||
//System.Diagnostics.Debug.WriteLine(c.Result);
|
||||
CurrentViewModel = _viewModelFactory.GetPart2ViewModel();
|
||||
ItHappened?.Invoke();
|
||||
currentDispatcher.Invoke(() =>
|
||||
{
|
||||
Strings.Add($"{DateTime.Now}");
|
||||
});
|
||||
},TaskContinuationOptions.OnlyOnRanToCompletion);
|
||||
}, TaskContinuationOptions.OnlyOnRanToCompletion);
|
||||
}
|
||||
|
||||
public event Action ItHappened;
|
||||
|
||||
private async Task<string> LongDelay()
|
||||
{
|
||||
await Task.Delay(500);
|
||||
|
||||
@@ -9,9 +9,9 @@ namespace WpfViewModelFirst.Part1
|
||||
{
|
||||
public class Part1ViewModel : ViewModelBase
|
||||
{
|
||||
private readonly Func<Task> _finishAction;
|
||||
private readonly Action _finishAction;
|
||||
|
||||
public Part1ViewModel(Func<Task> finishAction)
|
||||
public Part1ViewModel(Action finishAction)
|
||||
{
|
||||
_finishAction = finishAction;
|
||||
FinishCommand = new CustomCommand(Finish);
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace WpfViewModelFirst
|
||||
{
|
||||
public class ViewModelFactory : IViewModelFactory
|
||||
{
|
||||
public Part1.Part1ViewModel GetPart1ViewModel(Func<Task> finishAction)
|
||||
public Part1.Part1ViewModel GetPart1ViewModel(Action finishAction)
|
||||
{
|
||||
return new(finishAction);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user