Escali License control 1
UserServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System;
2using System.Threading.Tasks;
7using Microsoft.EntityFrameworkCore;
8using Xunit;
9
11{
12
16public class UserServiceTest
17{
18
24 private async Task<DataContext> InsertData(DataContext context)
25 {
26 context.Users.Add(new User() { UserEmail = "user@gmail.com", UserConsulentName = "Bob"});
27 await context.SaveChangesAsync();
28 return context;
29 }
30
35 [Fact]
36 public async void GetAllUsers_IsNotEmpty()
37 {
38
39 // Arrange
41 await InsertData(context);
42
43 // Act
44 var userService = new UserService(context);
45 var res = userService.GetAllUsers();
46
47 // Assert
48 Assert.NotEmpty(res.Result);
49
50 }
51
56 [Fact]
58 {
59
60 // Arrange
62 await InsertData(context);
63
64 var newUserWithSameEmailAsAlreadyInserted = new User() { UserEmail = "user@gmail.com" };
65 await context.SaveChangesAsync();
66
67 var userService = new UserService(context);
68
69 // Act
70 var op = async () => await userService.AddUser(newUserWithSameEmailAsAlreadyInserted);
71
72 // Assert
73 await Assert.ThrowsAsync<InvalidOperationException>(op);
74
75 }
76
81 [Fact]
82 public async void UpdateUser_True()
83 {
84 // Arrange
86 var user = context.Users.Add(new User() { UserEmail = "user@gmail.com", UserConsulentName = "Bob"});
87 await context.SaveChangesAsync();
88
89 // Act
90 var userService = new UserService(context);
91 var res = userService.GetAllUsers();
92
93 // Assert first email
94 Assert.Equal(user.Entity.UserEmail, res.Result[0].UserEmail);
95
96 // Act
97 user.Entity.UserEmail = "userUpdate@gmail.com";
98 context.Users.Update(user.Entity);
99 await userService.UpdateUser(user.Entity);
100
101 res = userService.GetAllUsers();
102
103 // Assert changed email
104 Assert.Equal("userUpdate@gmail.com", res.Result[0].UserEmail);
105
106 }
107
108 }
109}
var context
Definition: Program.cs:49
UserService class inserts and updates Users in the Database
Definition: UserService.cs:12
async Task< DataContext > InsertData(DataContext context)
Inserts an User into the DataContext
async void UpdateUser_True()
Adds an User to DataContext.UserService.Users and updates the email Will pass if updated email equals...
async void GetAllUsers_IsNotEmpty()
Adds an User to DataContext Will pass if userlist is not empty
async void AddUser_UserEmailAlreadyExists()
Adds an User to DataContext with email that already exist Will pass if InvalidOperationException is t...
static DataContext GetContext()
Creates a testcontext for testing
Definition: TestBase.cs:14