Unit tests added.
This commit is contained in:
parent
92933928f9
commit
bc06fcd9a2
@ -12,7 +12,7 @@ namespace eShopOnContainers.TestRunner.Windows
|
||||
{
|
||||
// Otherwise you need to ensure that the test assemblies will
|
||||
// become part of the app bundle
|
||||
AddTestAssembly(typeof(UnitTests.DummyTests).GetTypeInfo().Assembly);
|
||||
AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).GetTypeInfo().Assembly);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
using eShopOnContainers.Core.ViewModels.Base;
|
||||
using eShopOnContainers.Core.Validations;
|
||||
|
||||
namespace eShopOnContainers.UnitTests
|
||||
{
|
||||
public class MockViewModel : ViewModelBase
|
||||
{
|
||||
private ValidatableObject<string> _forename;
|
||||
private ValidatableObject<string> _surname;
|
||||
|
||||
public ValidatableObject<string> Forename
|
||||
{
|
||||
get
|
||||
{
|
||||
return _forename;
|
||||
}
|
||||
set
|
||||
{
|
||||
_forename = value;
|
||||
RaisePropertyChanged(() => Forename);
|
||||
}
|
||||
}
|
||||
|
||||
public ValidatableObject<string> Surname
|
||||
{
|
||||
get
|
||||
{
|
||||
return _surname;
|
||||
}
|
||||
set
|
||||
{
|
||||
_surname = value;
|
||||
RaisePropertyChanged(() => Surname);
|
||||
}
|
||||
}
|
||||
|
||||
public MockViewModel()
|
||||
{
|
||||
_forename = new ValidatableObject<string>();
|
||||
_surname = new ValidatableObject<string>();
|
||||
|
||||
_forename.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Forename is required." });
|
||||
_surname.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Surname name is required." });
|
||||
}
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
bool isValidForename = _forename.Validate();
|
||||
bool isValidSurname = _surname.Validate();
|
||||
return isValidForename && isValidSurname;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
using Xunit;
|
||||
using eShopOnContainers.Core.ViewModels.Base;
|
||||
|
||||
namespace eShopOnContainers.UnitTests
|
||||
{
|
||||
public class MockViewModelTests
|
||||
{
|
||||
[Fact]
|
||||
public void CheckValidationFailsWhenPropertiesAreEmptyTest()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
|
||||
bool isValid = mockViewModel.Validate();
|
||||
|
||||
Assert.False(isValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckValidationFailsWhenOnlyForenameHasDataTest()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
mockViewModel.Forename.Value = "John";
|
||||
|
||||
bool isValid = mockViewModel.Validate();
|
||||
|
||||
Assert.False(isValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckValidationPassesWhenOnlySurnameHasDataTest()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
mockViewModel.Surname.Value = "Smith";
|
||||
|
||||
bool isValid = mockViewModel.Validate();
|
||||
|
||||
Assert.False(isValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckValidationPassesWhenPropertiesHaveDataTest()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
mockViewModel.Forename.Value = "John";
|
||||
mockViewModel.Surname.Value = "Smith";
|
||||
|
||||
bool isValid = mockViewModel.Validate();
|
||||
|
||||
Assert.True(isValid);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SettingForenamePropertyShouldRaisePropertyChanged()
|
||||
{
|
||||
bool invoked = false;
|
||||
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
|
||||
mockViewModel.Forename.PropertyChanged += (sender, e) =>
|
||||
{
|
||||
if (e.PropertyName.Equals("Value"))
|
||||
invoked = true;
|
||||
};
|
||||
mockViewModel.Forename.Value = "John";
|
||||
|
||||
Assert.True(invoked);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SettingSurnamePropertyShouldRaisePropertyChanged()
|
||||
{
|
||||
bool invoked = false;
|
||||
|
||||
ViewModelLocator.RegisterDependencies(true);
|
||||
var mockViewModel = new MockViewModel();
|
||||
|
||||
mockViewModel.Surname.PropertyChanged += (sender, e) =>
|
||||
{
|
||||
if (e.PropertyName.Equals("Value"))
|
||||
invoked = true;
|
||||
};
|
||||
mockViewModel.Surname.Value = "Smith";
|
||||
|
||||
Assert.True(invoked);
|
||||
}
|
||||
}
|
||||
}
|
@ -43,6 +43,8 @@
|
||||
<Compile Include="ViewModels\OrderViewModelTests.cs" />
|
||||
<Compile Include="Services\OrdersServiceTests.cs" />
|
||||
<Compile Include="Behaviors\EventToCommandBehaviorTests.cs" />
|
||||
<Compile Include="Mocks\MockViewModel.cs" />
|
||||
<Compile Include="ViewModels\MockViewModelTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
|
||||
|
Loading…
x
Reference in New Issue
Block a user