comparison SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs @ 107:572886951353

Working tests under StatLight
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 16:09:28 +0100
parents 62477c2e8837
children
comparison
equal deleted inserted replaced
106:62477c2e8837 107:572886951353
1 using System; 1 using System;
2 using FluentAssertions.EventMonitoring; 2 using FluentAssertions.EventMonitoring;
3 using NUnit.Framework; 3 using GalaSoft.MvvmLight.Messaging;
4 using Microsoft.VisualStudio.TestTools.UnitTesting;
5 using SilverlightValidation.Messages;
4 using SilverlightValidation.Models; 6 using SilverlightValidation.Models;
5 using SilverlightValidation.Tests.Fakes; 7 using SilverlightValidation.Tests.Fakes;
6 using SilverlightValidation.Validators; 8 using SilverlightValidation.Validators;
7 using SilverlightValidation.ViewModels; 9 using SilverlightValidation.ViewModels;
8 10
9 namespace SilverlightValidation.Tests.ViewModels 11 namespace SilverlightValidation.Tests.ViewModels
10 { 12 {
11 [TestFixture] 13 [TestClass]
12 class UserViewModelFixture 14 public class UserViewModelFixture
13 { 15 {
14 #region Constructor 16 #region Constructor
15 17
16 [Test] 18 [TestMethod]
17 public void WhenConstructed_WithTwoNulls_ThenArgumentNullExceptionForModel() 19 [ExpectedException(typeof(ArgumentNullException))]
18 { 20 public void WhenConstructed_WithTwoNulls_ThenThrowsArgumentNullException()
19 Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, null), "model"); 21 {
20 } 22 new UserViewModel(null, null);
21 23 }
22 [Test] 24
23 public void WhenConstructed_WithNullFirstParam_ThenArgumentNullExceptionForModel() 25 [TestMethod]
24 { 26 [ExpectedException(typeof(ArgumentNullException))]
25 Assert.Throws<ArgumentNullException>(() => new UserViewModel(null, UserModelValidator.Create()), "model"); 27 public void WhenConstructed_WithNullFirstParam_ThenThrowsArgumentNullException()
26 } 28 {
27 29 new UserViewModel(null, UserModelValidator.Create());
28 [Test] 30 }
29 public void WhenConstructed_WithNullSecondParam_ThenArgumentNullExceptionForValidator() 31
30 { 32 [TestMethod]
31 Assert.Throws<ArgumentNullException>(() => new UserViewModel(UserModel.Create(), null), "validator"); 33 [ExpectedException(typeof(ArgumentNullException))]
32 } 34 public void WhenConstructed_WithNullSecondParam_ThenThrowsArgumentNullException()
33 35 {
34 [Test] 36 new UserViewModel(UserModel.Create(), null);
37 }
38
39 [TestMethod]
35 public void WhenConstructed_WithGenericParams_ThenInstantiatesViewModel() 40 public void WhenConstructed_WithGenericParams_ThenInstantiatesViewModel()
36 { 41 {
37 var vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create()); 42 var vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create());
38 Assert.IsNotNull(vm); 43 Assert.IsNotNull(vm);
39 } 44 }
40 45
41 #endregion 46 #endregion
42 47
43 #region INotifyPropertyChanged 48 #region INotifyPropertyChanged
44 49
45 [Test] 50 [TestMethod]
46 public void WhenPropertyChanged_WithValidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 51 public void WhenPropertyChanged_WithValidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
47 { 52 {
48 // arrange 53 // arrange
49 var vm = CreateDefaultUserViewModel(); 54 var vm = CreateDefaultUserViewModel();
50 55
55 vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth); 60 vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
56 Assert.IsTrue(vm.IsChanged); 61 Assert.IsTrue(vm.IsChanged);
57 } 62 }
58 63
59 64
60 [Test] 65 [TestMethod]
61 public void WhenPropertyChanged_WithInvalidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 66 public void WhenPropertyChanged_WithInvalidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
62 { 67 {
63 // arrange 68 // arrange
64 var vm = CreateDefaultUserViewModel(); 69 var vm = CreateDefaultUserViewModel();
65 70
69 // assert 74 // assert
70 vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth); 75 vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth);
71 Assert.IsTrue(vm.IsChanged); 76 Assert.IsTrue(vm.IsChanged);
72 } 77 }
73 78
74 [Test] 79 [TestMethod]
75 public void WhenPropertyChanged_WithDescriptionUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 80 public void WhenPropertyChanged_WithDescriptionUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
76 { 81 {
77 // arrange 82 // arrange
78 var vm = CreateDefaultUserViewModel(); 83 var vm = CreateDefaultUserViewModel();
79 84
83 // assert 88 // assert
84 vm.ShouldRaisePropertyChangeFor(x => x.Description); 89 vm.ShouldRaisePropertyChangeFor(x => x.Description);
85 Assert.IsTrue(vm.IsChanged); 90 Assert.IsTrue(vm.IsChanged);
86 } 91 }
87 92
88 [Test] 93 [TestMethod]
89 public void WhenPropertyChanged_WithValidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 94 public void WhenPropertyChanged_WithValidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
90 { 95 {
91 // arrange 96 // arrange
92 var vm = CreateDefaultUserViewModel(); 97 var vm = CreateDefaultUserViewModel();
93 98
97 // assert 102 // assert
98 vm.ShouldRaisePropertyChangeFor(x => x.Email); 103 vm.ShouldRaisePropertyChangeFor(x => x.Email);
99 Assert.IsTrue(vm.IsChanged); 104 Assert.IsTrue(vm.IsChanged);
100 } 105 }
101 106
102 [Test] 107 [TestMethod]
103 public void WhenPropertyChanged_WithInvalidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 108 public void WhenPropertyChanged_WithInvalidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
104 { 109 {
105 // arrange 110 // arrange
106 var vm = CreateDefaultUserViewModel(); 111 var vm = CreateDefaultUserViewModel();
107 112
111 // assert 116 // assert
112 vm.ShouldRaisePropertyChangeFor(x => x.Email); 117 vm.ShouldRaisePropertyChangeFor(x => x.Email);
113 Assert.IsTrue(vm.IsChanged); 118 Assert.IsTrue(vm.IsChanged);
114 } 119 }
115 120
116 [Test] 121 [TestMethod]
117 public void WhenPropertyChanged_WithValidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 122 public void WhenPropertyChanged_WithValidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
118 { 123 {
119 // arrange 124 // arrange
120 var vm = CreateDefaultUserViewModel(); 125 var vm = CreateDefaultUserViewModel();
121 126
125 // assert 130 // assert
126 vm.ShouldRaisePropertyChangeFor(x => x.Username); 131 vm.ShouldRaisePropertyChangeFor(x => x.Username);
127 Assert.IsTrue(vm.IsChanged); 132 Assert.IsTrue(vm.IsChanged);
128 } 133 }
129 134
130 [Test] 135 [TestMethod]
131 public void WhenPropertyChanged_WithInvalidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 136 public void WhenPropertyChanged_WithInvalidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
132 { 137 {
133 // arrange 138 // arrange
134 var vm = CreateDefaultUserViewModel(); 139 var vm = CreateDefaultUserViewModel();
135 140
139 // assert 144 // assert
140 vm.ShouldRaisePropertyChangeFor(x => x.Username); 145 vm.ShouldRaisePropertyChangeFor(x => x.Username);
141 Assert.IsTrue(vm.IsChanged); 146 Assert.IsTrue(vm.IsChanged);
142 } 147 }
143 148
144 [Test] 149 [TestMethod]
145 public void WhenPropertyChanged_WithValidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 150 public void WhenPropertyChanged_WithValidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
146 { 151 {
147 // arrange 152 // arrange
148 var vm = CreateDefaultUserViewModel(); 153 var vm = CreateDefaultUserViewModel();
149 154
153 // assert 158 // assert
154 vm.ShouldRaisePropertyChangeFor(x => x.Password); 159 vm.ShouldRaisePropertyChangeFor(x => x.Password);
155 Assert.IsTrue(vm.IsChanged); 160 Assert.IsTrue(vm.IsChanged);
156 } 161 }
157 162
158 [Test] 163 [TestMethod]
159 public void WhenPropertyChanged_WithInvalidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() 164 public void WhenPropertyChanged_WithInvalidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue()
160 { 165 {
161 // arrange 166 // arrange
162 var vm = CreateDefaultUserViewModel(); 167 var vm = CreateDefaultUserViewModel();
163 168
171 176
172 #endregion 177 #endregion
173 178
174 #region Commands 179 #region Commands
175 180
176 [Test] 181 [TestMethod]
177 public void WhenNewViewModel_ThenOkCommandIsExecutable() 182 public void WhenNewViewModel_ThenOkCommandIsExecutable()
178 { 183 {
179 // arrange 184 // arrange
180 var vm = CreateDefaultUserViewModel(); 185 var vm = CreateDefaultUserViewModel();
181 186
182 // assert 187 // assert
183 Assert.IsTrue(vm.OkCommand.CanExecute(null)); 188 Assert.IsTrue(vm.OkCommand.CanExecute(null));
184 } 189 }
185 190
186 [Test] 191 [TestMethod]
187 public void WhenNewViewModel_ThenCancelCommandIsExecutable() 192 public void WhenNewViewModel_ThenCancelCommandIsExecutable()
188 { 193 {
189 // arrange 194 // arrange
190 var vm = CreateDefaultUserViewModel(); 195 var vm = CreateDefaultUserViewModel();
191 196
192 // assert 197 // assert
193 Assert.IsTrue(vm.CancelCommand.CanExecute(null)); 198 Assert.IsTrue(vm.CancelCommand.CanExecute(null));
194 } 199 }
195 200
196 [Test] 201 [TestMethod]
197 public void WhenNewViewModel_WithCancelExecuted_ThenShouldRaiseEvent() 202 public void WhenNewViewModel_WithCancelExecuted_ThenShouldRaiseEvent()
198 { 203 {
199 // arrange 204 // arrange
200 var vm = CreateDefaultUserViewModel(); 205 var vm = CreateDefaultUserViewModel();
206 bool hasMessage = false;
207 Messenger.Default.Register<UserViewResponseMessage>(this, message => { hasMessage = true; });
201 208
202 // act 209 // act
203 vm.CancelCommand.Execute(null); 210 vm.CancelCommand.Execute(null);
204 211
205 // assert 212 // assert
206 vm.ShouldRaise("UserViewResponseMessage"); 213 Assert.IsTrue(hasMessage);
207 } 214 }
208 215
209 #endregion 216 #endregion
210 217
211 #region Helpers 218 #region Helpers