Skip to content

Commit bc06fcd

Browse files
David BritchDavid Britch
authored andcommitted
Unit tests added.
1 parent 9293392 commit bc06fcd

4 files changed

Lines changed: 148 additions & 1 deletion

File tree

src/Mobile/eShopOnContainers/eShopOnContainers.TestRunner.Windows/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected override void OnInitializeRunner()
1212
{
1313
// Otherwise you need to ensure that the test assemblies will
1414
// become part of the app bundle
15-
AddTestAssembly(typeof(UnitTests.DummyTests).GetTypeInfo().Assembly);
15+
AddTestAssembly(typeof(UnitTests.CatalogViewModelTests).GetTypeInfo().Assembly);
1616
}
1717
}
1818
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using eShopOnContainers.Core.ViewModels.Base;
2+
using eShopOnContainers.Core.Validations;
3+
4+
namespace eShopOnContainers.UnitTests
5+
{
6+
public class MockViewModel : ViewModelBase
7+
{
8+
private ValidatableObject<string> _forename;
9+
private ValidatableObject<string> _surname;
10+
11+
public ValidatableObject<string> Forename
12+
{
13+
get
14+
{
15+
return _forename;
16+
}
17+
set
18+
{
19+
_forename = value;
20+
RaisePropertyChanged(() => Forename);
21+
}
22+
}
23+
24+
public ValidatableObject<string> Surname
25+
{
26+
get
27+
{
28+
return _surname;
29+
}
30+
set
31+
{
32+
_surname = value;
33+
RaisePropertyChanged(() => Surname);
34+
}
35+
}
36+
37+
public MockViewModel()
38+
{
39+
_forename = new ValidatableObject<string>();
40+
_surname = new ValidatableObject<string>();
41+
42+
_forename.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Forename is required." });
43+
_surname.Validations.Add(new IsNotNullOrEmptyRule<string> { ValidationMessage = "Surname name is required." });
44+
}
45+
46+
public bool Validate()
47+
{
48+
bool isValidForename = _forename.Validate();
49+
bool isValidSurname = _surname.Validate();
50+
return isValidForename && isValidSurname;
51+
}
52+
}
53+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Xunit;
2+
using eShopOnContainers.Core.ViewModels.Base;
3+
4+
namespace eShopOnContainers.UnitTests
5+
{
6+
public class MockViewModelTests
7+
{
8+
[Fact]
9+
public void CheckValidationFailsWhenPropertiesAreEmptyTest()
10+
{
11+
ViewModelLocator.RegisterDependencies(true);
12+
var mockViewModel = new MockViewModel();
13+
14+
bool isValid = mockViewModel.Validate();
15+
16+
Assert.False(isValid);
17+
}
18+
19+
[Fact]
20+
public void CheckValidationFailsWhenOnlyForenameHasDataTest()
21+
{
22+
ViewModelLocator.RegisterDependencies(true);
23+
var mockViewModel = new MockViewModel();
24+
mockViewModel.Forename.Value = "John";
25+
26+
bool isValid = mockViewModel.Validate();
27+
28+
Assert.False(isValid);
29+
}
30+
31+
[Fact]
32+
public void CheckValidationPassesWhenOnlySurnameHasDataTest()
33+
{
34+
ViewModelLocator.RegisterDependencies(true);
35+
var mockViewModel = new MockViewModel();
36+
mockViewModel.Surname.Value = "Smith";
37+
38+
bool isValid = mockViewModel.Validate();
39+
40+
Assert.False(isValid);
41+
}
42+
43+
[Fact]
44+
public void CheckValidationPassesWhenPropertiesHaveDataTest()
45+
{
46+
ViewModelLocator.RegisterDependencies(true);
47+
var mockViewModel = new MockViewModel();
48+
mockViewModel.Forename.Value = "John";
49+
mockViewModel.Surname.Value = "Smith";
50+
51+
bool isValid = mockViewModel.Validate();
52+
53+
Assert.True(isValid);
54+
}
55+
56+
[Fact]
57+
public void SettingForenamePropertyShouldRaisePropertyChanged()
58+
{
59+
bool invoked = false;
60+
61+
ViewModelLocator.RegisterDependencies(true);
62+
var mockViewModel = new MockViewModel();
63+
64+
mockViewModel.Forename.PropertyChanged += (sender, e) =>
65+
{
66+
if (e.PropertyName.Equals("Value"))
67+
invoked = true;
68+
};
69+
mockViewModel.Forename.Value = "John";
70+
71+
Assert.True(invoked);
72+
}
73+
74+
[Fact]
75+
public void SettingSurnamePropertyShouldRaisePropertyChanged()
76+
{
77+
bool invoked = false;
78+
79+
ViewModelLocator.RegisterDependencies(true);
80+
var mockViewModel = new MockViewModel();
81+
82+
mockViewModel.Surname.PropertyChanged += (sender, e) =>
83+
{
84+
if (e.PropertyName.Equals("Value"))
85+
invoked = true;
86+
};
87+
mockViewModel.Surname.Value = "Smith";
88+
89+
Assert.True(invoked);
90+
}
91+
}
92+
}

src/Mobile/eShopOnContainers/eShopOnContainers.UnitTests/eShopOnContainers.UnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
<Compile Include="ViewModels\OrderViewModelTests.cs" />
4444
<Compile Include="Services\OrdersServiceTests.cs" />
4545
<Compile Include="Behaviors\EventToCommandBehaviorTests.cs" />
46+
<Compile Include="Mocks\MockViewModel.cs" />
47+
<Compile Include="ViewModels\MockViewModelTests.cs" />
4648
</ItemGroup>
4749
<ItemGroup>
4850
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">

0 commit comments

Comments
 (0)