Duplicated test scenario I would like to test
This commit is contained in:
34
WpfViewModelFirst.Tests/MainWindowViewModelTests.cs
Normal file
34
WpfViewModelFirst.Tests/MainWindowViewModelTests.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using Moq;
|
||||||
|
using WpfViewModelFirst.Part1;
|
||||||
|
using WpfViewModelFirst.Part2;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Tests
|
||||||
|
{
|
||||||
|
public class MainWindowViewModelTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Part1WillSetPart1ViewModelToCurrentViewModel()
|
||||||
|
{
|
||||||
|
Part1ViewModel part1ViewModel = new(() => _ = true);
|
||||||
|
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||||
|
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||||
|
.Returns(part1ViewModel);
|
||||||
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||||
|
main.Part1(null);
|
||||||
|
Assert.Equal(part1ViewModel, main.CurrentViewModel);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Part1CallbackIsSuccessfull()
|
||||||
|
{
|
||||||
|
Mock<IViewModelFactory> mockViewModelFactory = new Mock<IViewModelFactory>();
|
||||||
|
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Action>()))
|
||||||
|
.Returns(() => new Part1ViewModel(() => Task.Delay(0)))
|
||||||
|
.Callback<Action>(c => c.Invoke());
|
||||||
|
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
||||||
|
.Returns(() => new Part2ViewModel());
|
||||||
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
||||||
|
main.Part1(null);
|
||||||
|
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
WpfViewModelFirst.Tests/Part1/Part1ViewModelTests.cs
Normal file
13
WpfViewModelFirst.Tests/Part1/Part1ViewModelTests.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Tests.Part1
|
||||||
|
{
|
||||||
|
public class Part1ViewModelTests
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
13
WpfViewModelFirst.Tests/Part2/Part2ViewModelTests.cs
Normal file
13
WpfViewModelFirst.Tests/Part2/Part2ViewModelTests.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst.Tests.Part2
|
||||||
|
{
|
||||||
|
public class Part2ViewModelTests
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
1
WpfViewModelFirst.Tests/Usings.cs
Normal file
1
WpfViewModelFirst.Tests/Usings.cs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
global using Xunit;
|
||||||
29
WpfViewModelFirst.Tests/WpfViewModelFirst.Tests.csproj
Normal file
29
WpfViewModelFirst.Tests/WpfViewModelFirst.Tests.csproj
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0-windows</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.18.2" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.1.2">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\WpfViewModelFirst\WpfViewModelFirst.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||||||
# Visual Studio Version 17
|
# Visual Studio Version 17
|
||||||
VisualStudioVersion = 17.3.32804.467
|
VisualStudioVersion = 17.3.32804.467
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfViewModelFirst", "WpfViewModelFirst\WpfViewModelFirst.csproj", "{E5611330-98DE-4571-A035-65576B2F8D4B}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WpfViewModelFirst", "WpfViewModelFirst\WpfViewModelFirst.csproj", "{E5611330-98DE-4571-A035-65576B2F8D4B}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{90D6F27D-C403-4DF2-A570-D0DEECAC4062}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{90D6F27D-C403-4DF2-A570-D0DEECAC4062}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
README.md = README.md
|
README.md = README.md
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfViewModelFirst.Tests", "WpfViewModelFirst.Tests\WpfViewModelFirst.Tests.csproj", "{89753E08-BE0A-4FEA-B457-5486BB46EFEE}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -20,6 +22,10 @@ Global
|
|||||||
{E5611330-98DE-4571-A035-65576B2F8D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{E5611330-98DE-4571-A035-65576B2F8D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E5611330-98DE-4571-A035-65576B2F8D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E5611330-98DE-4571-A035-65576B2F8D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E5611330-98DE-4571-A035-65576B2F8D4B}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E5611330-98DE-4571-A035-65576B2F8D4B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{89753E08-BE0A-4FEA-B457-5486BB46EFEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{89753E08-BE0A-4FEA-B457-5486BB46EFEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{89753E08-BE0A-4FEA-B457-5486BB46EFEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{89753E08-BE0A-4FEA-B457-5486BB46EFEE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace WpfViewModelFirst
|
|||||||
base.OnStartup(e);
|
base.OnStartup(e);
|
||||||
MainWindow mainWindow = new()
|
MainWindow mainWindow = new()
|
||||||
{
|
{
|
||||||
DataContext = new MainWindowViewModel()
|
DataContext = new MainWindowViewModel(new ViewModelFactory())
|
||||||
};
|
};
|
||||||
mainWindow.Show();
|
mainWindow.Show();
|
||||||
}
|
}
|
||||||
|
|||||||
12
WpfViewModelFirst/IViewModelFactory.cs
Normal file
12
WpfViewModelFirst/IViewModelFactory.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using WpfViewModelFirst.Part1;
|
||||||
|
using WpfViewModelFirst.Part2;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst
|
||||||
|
{
|
||||||
|
public interface IViewModelFactory
|
||||||
|
{
|
||||||
|
Part1ViewModel GetPart1ViewModel(Action finishAction);
|
||||||
|
Part2ViewModel GetPart2ViewModel();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,17 +12,20 @@ namespace WpfViewModelFirst
|
|||||||
{
|
{
|
||||||
public class MainWindowViewModel : ViewModelBase
|
public class MainWindowViewModel : ViewModelBase
|
||||||
{
|
{
|
||||||
public MainWindowViewModel()
|
public MainWindowViewModel(IViewModelFactory viewModelFactory)
|
||||||
{
|
{
|
||||||
|
_viewModelFactory = viewModelFactory;
|
||||||
StartCommand = new CustomCommand(Part1);
|
StartCommand = new CustomCommand(Part1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IViewModelFactory _viewModelFactory;
|
||||||
|
|
||||||
public ICommand StartCommand { get; set; }
|
public ICommand StartCommand { get; set; }
|
||||||
|
|
||||||
public void Part1(object? e)
|
public void Part1(object? e)
|
||||||
{
|
{
|
||||||
CurrentViewModel = null;
|
CurrentViewModel = null;
|
||||||
CurrentViewModel = new Part1ViewModel(Part1Action);
|
CurrentViewModel = _viewModelFactory.GetPart1ViewModel(Part1Action);
|
||||||
// MainFrameViewModel = new Part2ViewModel();
|
|
||||||
}
|
}
|
||||||
private void Part1Action()
|
private void Part1Action()
|
||||||
{
|
{
|
||||||
@@ -30,7 +33,7 @@ namespace WpfViewModelFirst
|
|||||||
Task delay = Task.Run(LongDelay).ContinueWith(c =>
|
Task delay = Task.Run(LongDelay).ContinueWith(c =>
|
||||||
{
|
{
|
||||||
System.Diagnostics.Debug.WriteLine(c.Result);
|
System.Diagnostics.Debug.WriteLine(c.Result);
|
||||||
CurrentViewModel = new Part2ViewModel();
|
CurrentViewModel = _viewModelFactory.GetPart2ViewModel();
|
||||||
}, TaskContinuationOptions.OnlyOnRanToCompletion);
|
}, TaskContinuationOptions.OnlyOnRanToCompletion);
|
||||||
//Task.Run(async () => await LongDelay()).ContinueWith(c =>
|
//Task.Run(async () => await LongDelay()).ContinueWith(c =>
|
||||||
// {
|
// {
|
||||||
|
|||||||
20
WpfViewModelFirst/ViewModelFactory.cs
Normal file
20
WpfViewModelFirst/ViewModelFactory.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WpfViewModelFirst
|
||||||
|
{
|
||||||
|
public class ViewModelFactory : IViewModelFactory
|
||||||
|
{
|
||||||
|
public Part1.Part1ViewModel GetPart1ViewModel(Action finishAction)
|
||||||
|
{
|
||||||
|
return new(finishAction);
|
||||||
|
}
|
||||||
|
public Part2.Part2ViewModel GetPart2ViewModel()
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user