Escali License control 1
ProductServiceTest.cs
Gå til dokumentasjonen til denne filen.
1using System;
5using Microsoft.EntityFrameworkCore;
6using Xunit;
7using Xunit.Abstractions;
8
10{
11
16{
17 private readonly ITestOutputHelper _testOutputHelper;
18
19 public ProductServiceTest(ITestOutputHelper testOutputHelper)
20 {
21 _testOutputHelper = testOutputHelper;
22 }
23
28 [Fact]
29 public async void GetAllProducts_IsEmpty()
30 {
31
32 // Arrange
34
35 // Act
36 var productService = new ProductService(context);
37 var res = await productService.GetAllProducts();
38
39 // Assert
40 Assert.Empty(res);
41
42 }
43
48 [Fact]
50 {
51
52 // Arrange
55
56 // Act
57 var productService = new ProductService(context);
58 var res = await productService.GetAllProducts();
59
60 // Assert
61 Assert.NotEmpty(res);
62 Assert.Equal("BestProductEver", res[0].ProductName);
63
64 }
65
69 [Fact]
71 {
72
73 // Arrange
75
76 // Act
77 var productService = new ProductService(context);
78 var res = await productService.GetProductById(100);
79
80 // Assert
81 Assert.Null(res);
82
83 }
84
88 [Fact]
89 public async void AddProduct_Success()
90 {
91
92 // Arrange
94 Product product = TestData.CreateProduct();
95 var productService = new ProductService(context);
96
97 // Act
98 var res = await productService.AddProduct(product);
99
100 // Assert
101 Assert.Equal(product.ProductName, res.ProductName);
102
103 }
104
108 [Fact]
110 {
111
112 // Arrange
114 var product = TestData.CreateProduct();
115 var addedProduct = context.Products.Add(product);
116 await context.SaveChangesAsync();
117
118 var productService = new ProductService(context);
119
120 // Act
121 var op = async () => await productService.AddProduct(product);
122
123 // Assert
124 await Assert.ThrowsAsync<InvalidOperationException>(op);
125
126 }
127
131 [Fact]
133 {
135
136 var service = new ProductService(context);
137 var product = new Product()
138 {
139 ProductSeq = 100,
140 ProductName = "Fake Product"
141 };
142
143 var op = async () => await service.UpdateProduct(product);
144
145 await Assert.ThrowsAsync<DbUpdateConcurrencyException>(op);
146
147 }
148
152 [Fact]
153 public async void UpdateProduct_SameName()
154 {
156
157 var service = new ProductService(context);
158 var product = new Product()
159 {
160 ProductName = "Fake Product"
161 };
162 var added = context.Products.Add(product);
163 await context.SaveChangesAsync();
164
165 var fromDb = await context.Products.FindAsync(1);
166 fromDb.ProductName = "Fake Product";
167
168 var op = async () => await service.UpdateProduct(fromDb);
169
170 // await Assert.ThrowsAsync<DbUpdateConcurrencyException>(op);
171
172 await op.Invoke();
173 }
174
178 [Fact]
180 {
182
183 var service = new ProductService(context);
184 var product = new Product()
185 {
186 ProductName = "Fake Product"
187 };
188 var added = context.Products.Add(product);
189 await context.SaveChangesAsync();
190
191 var fromDb = await context.Products.FindAsync(1);
192 fromDb!.ProductName = "New";
193 await context.SaveChangesAsync();
194
195 var res = await context.Products.FindAsync(fromDb.ProductSeq);
196
197 Assert.Equal("New", res.ProductName);
198 }
199
200 }
201}
var context
Definition: Program.cs:49
var service
Definition: Program.cs:48
ProductService class inserts and updates Product in the Database
async void UpdateProduct_ProductNotExisting()
passes if product is not existing in productlist and throws a DbUpdateConcurrencyException
async void UpdateProduct_NewNameSuccess()
passes if productname is correctly changed in the productlist
async void GetAllProducts_IsEmpty()
Retrieves productlist Passes if productlist is empty
async void GetAllProducts_NotEmptyAndCorrectObject()
Inserts product in productlist passes if productlist is not empty and the inserted product equals the...
async void AddProduct_Success()
passes if product is correctly added to productlist
async void GetProductById_ProductNotExisting()
passes if product does not exist in the productlist
async void AddProduct_ProductNameAlreadyExists()
passes if productname already exist in productlist and throws an InvalidOperationException
static DataContext GetContext()
Creates a testcontext for testing
Definition: TestBase.cs:14
Reusable methods for inserting information into tests
Definition: TestData.cs:11
static DataContext InsertProduct(DataContext context)
Inserts Product into the context Used for inserting product into the test environment
Definition: TestData.cs:28
static Product CreateProduct()
Creates a Product
Definition: TestData.cs:17