changeset 102:db05a55e3536

INPC tests added
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 12:13:29 +0100
parents 2372846797f0
children 8cb4f36717e9
files SilverlightValidation/Libs/Moq.dll SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs SilverlightValidation/SilverlightValidation.Tests/Fakes/UserModelValidatorFake.cs SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserListViewModelFixture.cs SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs SilverlightValidation/SilverlightValidation.Tests/ViewModels/ViewModelBaseFixture.cs SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs
diffstat 12 files changed, 290 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
Binary file SilverlightValidation/Libs/Moq.dll has changed
--- a/SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs	Sun May 06 12:13:29 2012 +0100
@@ -10,11 +10,11 @@
         {
             return new List<UserModel>(5)
             {
-                new UserModel() { Username = "StevenH", Password = "Password1", Email = "steven@hotmail.com", DateOfBirth = new DateTime(1977, 09, 01), Description = ""},
-                new UserModel() { Username = "RichardJ", Password = "12N456a", Email = "dicky@gmail.com", DateOfBirth = new DateTime(1983, 03, 13), Description = "Loves .Net!"},
-                new UserModel() { Username = "BobbyP", Password = "pa33Word", Email = "bob@yahoo.co.uk", DateOfBirth = new DateTime(1992, 08, 30), Description = ""},
-                new UserModel() { Username = "DavidM", Password = "poIu789", Email = "daveyboy@marsh.com", DateOfBirth = new DateTime(1965, 06, 21), Description = "Java fan boy"},
-                new UserModel() { Username = "JessieJ", Password = "jlkJh567", Email = "jj@apple.co.uk", DateOfBirth = new DateTime(1990, 10, 15), Description = ""}
+                UserModel.Create("StevenH", "Password1", "steven@hotmail.com", new DateTime(1977, 09, 01), ""),
+                UserModel.Create("RichardJ", "12N456a", "dicky@gmail.com", new DateTime(1983, 03, 13), "Loves .Net!"),
+                UserModel.Create("BobbyP", "pa33Word", "bob@yahoo.co.uk", new DateTime(1992, 08, 30), ""),
+                UserModel.Create("DavidM", "poIu789", "daveyboy@marsh.com", new DateTime(1965, 06, 21), "Java fan boy"),
+                UserModel.Create("JessieJ", "jlkJh567", "jj@apple.co.uk", new DateTime(1990, 10, 15), "")
             };
         }
     }
--- a/SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs	Sun May 06 12:13:29 2012 +0100
@@ -1,5 +1,4 @@
 using System;
-using System.ComponentModel;
 using SilverlightValidation.Interfaces;
 
 namespace SilverlightValidation.Models
@@ -7,19 +6,32 @@
     public class UserModel : IUserModel
     {
         public string Username { get; set; }
+        public string Password { get; set; }
         public string Email { get; set; }
-        public string Password { get; set; }
         public DateTime? DateOfBirth { get; set; }
         public string Description { get; set; }
 
-        public static UserModel Create()
+        private UserModel() { }
+
+        public static UserModel Create(string username = "",
+                                        string password = "",
+                                        string email = "",
+                                        DateTime? dateOfBirth = null,
+                                        string descripton = "")
         {
-            return new UserModel() { Username = "", Email = "", Password = "", DateOfBirth = null, Description = "" };
+            return new UserModel()
+                       {
+                           Username = "",
+                           Password = "",
+                           Email = "",
+                           DateOfBirth = null,
+                           Description = ""
+                       };
         }
 
         public IUserModel Clone()
         {
-            return (UserModel) this.MemberwiseClone(); 
+            return (UserModel)this.MemberwiseClone();
         }
     }
 }
--- a/SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs	Sun May 06 12:13:29 2012 +0100
@@ -6,7 +6,7 @@
 {
     public class UserModelValidator : AbstractValidator<IUserModel>
     {
-        public UserModelValidator()
+        private UserModelValidator()
         {
             RuleFor(x => x.Username)
                 .Length(3, 8)
@@ -32,5 +32,10 @@
                 return false;
             return true;
         }
+
+        public static UserModelValidator Create()
+        {
+            return new UserModelValidator();
+        }
     }
 }
--- a/SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs	Sun May 06 12:13:29 2012 +0100
@@ -5,7 +5,6 @@
 using System.Windows.Input;
 using FluentValidation;
 using SilverlightValidation.Interfaces;
-using SilverlightValidation.Validators;
 using SilverlightValidation.Models;
 using SilverlightValidation.Commands;
 using GalaSoft.MvvmLight.Messaging;
@@ -17,7 +16,7 @@
     {
         #region Fields
 
-        private readonly UserModelValidator _validator;
+        private readonly AbstractValidator<IUserModel> _validator;
         private UserModel _data;
         private UserModel _backup;
 
@@ -25,7 +24,7 @@
 
         #region Constructor
 
-        public UserViewModel(UserModel model, UserModelValidator validator)
+        public UserViewModel(UserModel model, AbstractValidator<IUserModel> validator)
         {
             if (model == null) throw new ArgumentNullException("model");
             if (validator == null) throw new ArgumentNullException("validator");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SilverlightValidation/SilverlightValidation.Tests/Fakes/UserModelValidatorFake.cs	Sun May 06 12:13:29 2012 +0100
@@ -0,0 +1,38 @@
+using System;
+using FluentValidation;
+using SilverlightValidation.Interfaces;
+
+namespace SilverlightValidation.Tests.Fakes
+{
+    class UserModelValidatorFake : AbstractValidator<IUserModel>
+    {
+        private UserModelValidatorFake()
+        {
+            RuleFor(x => x.Username)
+                .Length(3, 8)
+                .WithMessage("Must be between 3-8 characters.");
+
+            RuleFor(x => x.Password)
+                .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$")
+                .WithMessage("Must contain lower, upper and numeric chars.");
+
+            RuleFor(x => x.Email)
+                .EmailAddress()
+                .WithMessage("A valid email address is required.");
+
+            RuleFor(x => x.DateOfBirth)
+                .Must(BeAValidDateOfBirth)
+                .WithMessage("Must be within 100 years of today.");
+        }
+
+        private bool BeAValidDateOfBirth(DateTime? dateOfBirth)
+        {
+            if (dateOfBirth == null) return false;
+            if (dateOfBirth.Value > DateTime.Today || dateOfBirth < DateTime.Today.AddYears(-100))
+                return false;
+            return true;
+        }
+
+        public static UserModelValidatorFake Create() { return new UserModelValidatorFake(); }
+    }
+}
--- a/SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj	Sun May 06 12:13:29 2012 +0100
@@ -31,6 +31,9 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="FakeItEasy">
+      <HintPath>..\Libs\FakeItEasy.dll</HintPath>
+    </Reference>
     <Reference Include="FluentAssertions">
       <HintPath>..\Libs\FluentAssertions.dll</HintPath>
     </Reference>
@@ -52,10 +55,11 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="ViewModels\UserListViewModelTests.cs" />
+    <Compile Include="Fakes\UserModelValidatorFake.cs" />
+    <Compile Include="ViewModels\UserListViewModelFixture.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="ViewModels\UserViewModelTests.cs" />
-    <Compile Include="ViewModels\ViewModelBaseTests.cs" />
+    <Compile Include="ViewModels\UserViewModelFixture.cs" />
+    <Compile Include="ViewModels\ViewModelBaseFixture.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\SilverlightValidation.PL\SilverlightValidation.PL.csproj">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserListViewModelFixture.cs	Sun May 06 12:13:29 2012 +0100
@@ -0,0 +1,15 @@
+using System;
+using NUnit.Framework;
+
+namespace SilverlightValidation.Tests.ViewModels
+{
+    [TestFixture]
+    class UserListViewModelFixture
+    {
+        [Test]
+        public void Given_When_Then()
+        {
+            Assert.True(true);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs	Sun May 06 12:13:29 2012 +0100
@@ -0,0 +1,185 @@
+using System;
+using FluentAssertions.EventMonitoring;
+using NUnit.Framework;
+using SilverlightValidation.Models;
+using SilverlightValidation.Tests.Fakes;
+using SilverlightValidation.Validators;
+using SilverlightValidation.ViewModels;
+
+namespace SilverlightValidation.Tests.ViewModels
+{
+    [TestFixture]
+    class UserViewModelFixture
+    {
+        #region Constructor
+
+        [Test]
+        public void WhenConstructed_WithTwoNulls_ThenArgumentNullExceptionForModel()
+        {
+            Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, null), "model");
+        }
+
+        [Test]
+        public void WhenConstructed_WithNullFirstParam_ThenArgumentNullExceptionForModel()
+        {
+            Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, UserModelValidator.Create()), "model");
+        }
+
+        [Test]
+        public void WhenConstructed_WithNullSecondParam_ThenArgumentNullExceptionForValidator()
+        {
+            Assert.Throws<ArgumentNullException>(() => new UserViewModel(UserModel.Create(), null), "validator");
+        }
+
+        [Test]
+        public void WhenConstructed_WithGenericParams_ThenInstantiatesViewModel()
+        {
+            var vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create());
+            Assert.IsNotNull(vm);
+        }
+
+        #endregion
+
+        #region INotifyPropertyChanged
+
+        [Test]
+        public void WhenPropertyChanged_WithValidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.DateOfBirth = new DateTime(1977, 01, 01);
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+
+        [Test]
+        public void WhenPropertyChanged_WithInvalidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.DateOfBirth = new DateTime(1800, 01, 01);
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithDescriptionUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+            
+            // act
+            vm.Description = "New description";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Description);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithValidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Email = "test@domain.com";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Email);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithInvalidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Email = "invalid email";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Email);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithValidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Username = "dummy";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Username);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithInvalidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Username = "invalidusernametoolong";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Username);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithValidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Password = "dummy";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Password);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        [Test]
+        public void WhenPropertyChanged_WithInvalidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
+        {
+            // arrange
+            var vm = CreateDefaultUserViewModel();
+
+            // act
+            vm.Password = "invalidpasswordtoolong";
+
+            // assert
+            vm.ShouldRaisePropertyChangeFor(x => x.Password);
+            Assert.IsTrue(vm.IsChanged);
+        }
+
+        #endregion
+
+        #region Helpers
+
+        private static UserViewModel CreateDefaultUserViewModel()
+        {
+            var vm = new UserViewModel(UserModel.Create(), UserModelValidatorFake.Create());
+            vm.MonitorEvents();
+            return vm;
+        }
+
+        #endregion
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/ViewModelBaseFixture.cs	Sun May 06 12:13:29 2012 +0100
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using NUnit.Framework;
+
+namespace SilverlightValidation.Tests.ViewModels
+{
+    [TestFixture]
+    class ViewModelBaseFixture
+    {
+        
+    }
+}
--- a/SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs	Sun May 06 12:13:29 2012 +0100
@@ -15,7 +15,7 @@
             InitializeComponent();
             HtmlPage.Document.SetProperty("title", "Silverlight Validation");
 
-            vm = new UserListViewModel(new UserView(), Factory.CreateUserModels(), new UserModelValidator());
+            vm = new UserListViewModel(new UserView(), Factory.CreateUserModels(), UserModelValidator.Create());
             this.DataContext = vm;
         }
 
--- a/SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs	Sat May 05 23:04:42 2012 +0100
+++ b/SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs	Sun May 06 12:13:29 2012 +0100
@@ -15,7 +15,7 @@
             InitializeComponent();
             HtmlPage.Document.SetProperty("title", "Silverlight Validation");
 
-            vm = new UserViewModel(UserModel.Create(), new UserModelValidator());
+            vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create());
             this.DataContext = vm;
         }