Unit tests added.

This commit is contained in:
David Britch 2017-04-25 11:07:35 +01:00
parent 92933928f9
commit bc06fcd9a2
4 changed files with 148 additions and 1 deletions

View File

@ -12,7 +12,7 @@ namespace eShopOnContainers.TestRunner.Windows
{ {
// Otherwise you need to ensure that the test assemblies will // Otherwise you need to ensure that the test assemblies will
// become part of the app bundle // become part of the app bundle
AddTestAssembly(typeof(UnitTests.DummyTests).GetTypeInfo().Assembly); AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).GetTypeInfo().Assembly);
} }
} }
} }

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -43,6 +43,8 @@
<Compile Include="ViewModels\OrderViewModelTests.cs" /> <Compile Include="ViewModels\OrderViewModelTests.cs" />
<Compile Include="Services\OrdersServiceTests.cs" /> <Compile Include="Services\OrdersServiceTests.cs" />
<Compile Include="Behaviors\EventToCommandBehaviorTests.cs" /> <Compile Include="Behaviors\EventToCommandBehaviorTests.cs" />
<Compile Include="Mocks\MockViewModel.cs" />
<Compile Include="ViewModels\MockViewModelTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL"> <Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">