In this post, we will see how to use Moq in order to create Unit Test for a Class Library project used to manage CRUD operations.
But, what is Moq?
Moq is a library used to create mock objects that simulate or mimic a real object.
Here, we can find the Moq project.
First of all, we define the User entity:
[USER.CS]
1 2 3 4 5 6 7 8 9 10 | using System; namespace Domain.Entities { public class User { public Guid Id { get ; set ; } public string UserName { get ; set ; } } } |
Then, we define the Interface used to define the repository:
[IUSERREPOSITORY.CS]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | using Domain.Entities; using System; using System.Collections.Generic; namespace Domain.DataAccess.Interfaces { public interface IUserRepository { List<User> GetAll(); User GetById(Guid id); bool Add(User user); bool Delete(Guid id); bool Update(User user); } } |
Finally, we define the class UserCore used to manage the CRUD operations:
[USERCORE.CS]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | using Domain.DataAccess.Interfaces; using Domain.Entities; using System; using System.Collections.Generic; using System.Linq; namespace Domain.Command { public class UserCore { private readonly IUserRepository _userRepository; public UserCore(IUserRepository userRepository) { _userRepository = userRepository; } public List<User> GetAllUsers( out string message) { message = string .Empty; List<User> lstUsers = null ; try { lstUsers = _userRepository.GetAll(); } catch (Exception ex) { message = ex.Message; } return lstUsers; } public User GetUserById(Guid id, out string message) { message = string .Empty; User user = null ; try { user = _userRepository.GetById(id); } catch (Exception ex) { message = ex.Message; } return user; } public bool AddUser(User user, out string message) { message = string .Empty; bool result = false ; User checkUser = _userRepository.GetAll().Where(x => x.UserName == user.UserName).FirstOrDefault(); if (checkUser!= null ) { message = "UserName already exists!" ; return result; } try { result = _userRepository.Add(user); } catch (Exception ex) { message = ex.Message; } return result; } public bool UpdateUser(User user, out string message) { message = string .Empty; bool result = false ; User checkUser = _userRepository.GetAll().Where(x => x.UserName == user.UserName && x.Id!=user.Id).FirstOrDefault(); if (checkUser != null ) { message = "UserName already exists!" ; return result; } try { result = _userRepository.Update(user); } catch (Exception ex) { message = ex.Message; } return result; } public bool DeleteUser(Guid id, out string message) { message = string .Empty; bool result = false ; User checkUser = _userRepository.GetById(id); if (checkUser == null ) { message = "User not exists!" ; return result; } try { result = _userRepository.Delete(id); } catch (Exception ex) { message = ex.Message; } return result; } } } |
We have finished to create our Class Library and now, we create an UnitTest project where we will add the library Moq.
Finally, we define the class UnitTest_UserCore where we will create all tests:
[UNITTEST_USERCORE.CS]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | using Domain.Command; using Domain.DataAccess.Interfaces; using Domain.Entities; using Moq; using System; using System.Collections.Generic; using Xunit; namespace UnitTest { public class UnitTest_UserCore { private readonly Mock<IUserRepository> userRepository; private readonly UserCore objUserCore; public UnitTest_UserCore() { userRepository = new Mock<IUserRepository>(); objUserCore = new UserCore(userRepository.Object); } [Fact] public void GetAllUsers_ShouldReturnListOfUser() { // Arrange // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName= "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // Act // run the method var result = objUserCore.GetAllUsers( out string message); // Assert Assert.Equal(lstUsers.Count, result.Count); Assert.Empty(message); } [Fact] public void GetAllUsers_WithAnException_ShouldReturnAnErrorMessage() { // Arrange // we define an exception when we call GetAll method userRepository.Setup(setup => setup.GetAll()).Throws( new Exception()); // Act // run the method var result = objUserCore.GetAllUsers( out string message); // Assert Assert.Null(result); Assert.NotEmpty(message); } [Fact] public void GetUserById_ShouldReturnAnUser() { // Arrange Guid idUser = Guid.NewGuid(); // we create the moq for the method GetById // 1) we define the output User objUsers = new User { Id = idUser, UserName = "UserName_10" }; // 2) we define objUsers as the output of GetById method userRepository.Setup(setup => setup.GetById(idUser)).Returns(objUsers); // Act // run the method var result = objUserCore.GetUserById(idUser, out string message); // Assert Assert.NotNull(result); Assert.Equal(idUser, result.Id); Assert.Empty(message); } [Fact] public void GetUserById_WithAnException_ShouldReturnErrorMessage() { // Arrange Guid idUser = Guid.NewGuid(); // we define an exception when we call GetById method userRepository.Setup(setup => setup.GetById(idUser)).Throws( new Exception()); // Act // run the method var result = objUserCore.GetUserById(idUser, out string message); // Assert Assert.Null(result); Assert.NotEmpty(message); } [Fact] public void AddUser_ShouldReturnTrue() { // Arrange User objUsers = new User { UserName = "UserName_10" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // 3) we define the output for the method Add userRepository.Setup(setup => setup.Add(objUsers)).Returns( true ); // Act // run the method var result = objUserCore.AddUser(objUsers, out string message); // Assert Assert.True(result); Assert.Empty(message); } [Fact] public void AddUser_WithTheUserNameAlreadyPresent_ShouldReturnFalseWithErrorMessage() { // Arrange User objUsers = new User { UserName = "UserName_2" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // Act // run the method var result = objUserCore.AddUser(objUsers, out string message); // Assert Assert.False(result); Assert.Equal( "UserName already exists!" , message); } [Fact] public void AddUser_WithAnException_ShouldReturnFalseWithErrorMessage() { // Arrange User objUsers = new User { UserName = "UserName_10" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // 3) we define the output for the method Add userRepository.Setup(setup => setup.Add(objUsers)).Throws( new Exception()); // Act // run the method var result = objUserCore.AddUser(objUsers, out string message); // Assert Assert.False(result); Assert.NotEmpty(message); } [Fact] public void UpdateUser_ShouldReturnTrue() { // Arrange Guid idUser = Guid.NewGuid(); User objUsers = new User { Id= idUser, UserName = "UserName_10" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = idUser, UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // 3) we define the output for the method Update userRepository.Setup(setup => setup.Update(objUsers)).Returns( true ); // Act // run the method var result = objUserCore.UpdateUser(objUsers, out string message); // Assert Assert.True(result); Assert.Empty(message); } [Fact] public void UpdateUser_WithTheUserNameAlreadyPresent_ShouldReturnFalseWithErrorMessage() { // Arrange Guid idUser = Guid.NewGuid(); User objUsers = new User { Id = idUser, UserName = "UserName_1" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = idUser, UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // Act // run the method var result = objUserCore.UpdateUser(objUsers, out string message); // Assert Assert.False(result); Assert.Equal( "UserName already exists!" , message); } [Fact] public void UpdateUser_WithAnException_ShouldReturnFalseWithErrorMessage() { // Arrange Guid idUser = Guid.NewGuid(); User objUsers = new User { Id = idUser, UserName = "UserName_10" }; // we create the moq for the method GetAll // 1) we define the output List<User> lstUsers = new List<User>(); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_1" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_2" }); lstUsers.Add( new User { Id = idUser, UserName = "UserName_3" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_4" }); lstUsers.Add( new User { Id = Guid.NewGuid(), UserName = "UserName_5" }); // 2) we define lstUser as the output of GetAll method userRepository.Setup(setup => setup.GetAll()).Returns(lstUsers); // 3) we define the output for the method Update userRepository.Setup(setup => setup.Update(objUsers)).Throws( new Exception()); // Act // run the method var result = objUserCore.UpdateUser(objUsers, out string message); // Assert Assert.False(result); Assert.NotEmpty(message); } [Fact] public void DeleteUser_ShouldReturnTrue() { // Arrange Guid idUser = Guid.NewGuid(); // we create the moq for the method GetById // 1) we define the output User objUsers = new User { Id = idUser, UserName = "UserName_1" }; // 2) we define objUsers as the output of GetById method userRepository.Setup(setup => setup.GetById(idUser)).Returns(objUsers); // 3) we define the output for the method Delete userRepository.Setup(setup => setup.Delete(idUser)).Returns( true ); // Act // run the method var result = objUserCore.DeleteUser(idUser, out string message); // Assert Assert.True(result); Assert.Empty(message); } [Fact] public void DeleteUser_WithAWrongIdUserShouldReturnFalseWithErrorMessage() { // Arrange Guid idUser = Guid.NewGuid(); // we create the moq for the method GetById // 1) we define the output User objUsers = null ; // 2) we define objUsers as the output of GetById method userRepository.Setup(setup => setup.GetById(idUser)).Returns(objUsers); // Act // run the method var result = objUserCore.DeleteUser(idUser, out string message); // Assert Assert.False(result); Assert.Equal( "User not exists!" , message); } [Fact] public void DeleteUser_WithAnException_ShouldReturnFalseWithErrorMessage() { // Arrange Guid idUser = Guid.NewGuid(); // we create the moq for the method GetById // 1) we define the output User objUsers = new User { Id = idUser, UserName = "UserName_1" }; // 2) we define objUsers as the output of GetById method userRepository.Setup(setup => setup.GetById(idUser)).Returns(objUsers); // 3) we define the output for the method Delete userRepository.Setup(setup => setup.Delete(idUser)).Throws( new Exception()); // Act // run the method var result = objUserCore.DeleteUser(idUser, out string message); // Assert Assert.False(result); Assert.NotEmpty(message); } } } |
We have done and now we can run the test:
