5using Microsoft.EntityFrameworkCore;
7using Xunit.Abstractions;
37 var res = await productService.GetAllProducts();
58 var res = await productService.GetAllProducts();
62 Assert.Equal(
"BestProductEver", res[0].ProductName);
78 var res = await productService.GetProductById(100);
98 var res = await productService.AddProduct(product);
101 Assert.Equal(product.
ProductName, res.ProductName);
115 var addedProduct =
context.Products.Add(product);
116 await
context.SaveChangesAsync();
121 var op = async () => await productService.AddProduct(product);
124 await Assert.ThrowsAsync<InvalidOperationException>(op);
140 ProductName =
"Fake Product"
143 var op = async () => await
service.UpdateProduct(product);
145 await Assert.ThrowsAsync<DbUpdateConcurrencyException>(op);
160 ProductName =
"Fake Product"
162 var added =
context.Products.Add(product);
163 await
context.SaveChangesAsync();
165 var fromDb = await
context.Products.FindAsync(1);
166 fromDb.ProductName =
"Fake Product";
168 var op = async () => await
service.UpdateProduct(fromDb);
186 ProductName =
"Fake Product"
188 var added =
context.Products.Add(product);
189 await
context.SaveChangesAsync();
191 var fromDb = await
context.Products.FindAsync(1);
192 fromDb!.ProductName =
"New";
193 await
context.SaveChangesAsync();
195 var res = await
context.Products.FindAsync(fromDb.ProductSeq);
197 Assert.Equal(
"New", res.ProductName);
ProductService class inserts and updates Product in the Database
ProductService Testing Environment
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
readonly ITestOutputHelper _testOutputHelper
async void GetAllProducts_NotEmptyAndCorrectObject()
Inserts product in productlist passes if productlist is not empty and the inserted product equals the...
ProductServiceTest(ITestOutputHelper testOutputHelper)
async void UpdateProduct_SameName()
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
Reusable methods for inserting information into tests
static DataContext InsertProduct(DataContext context)
Inserts Product into the context Used for inserting product into the test environment
static Product CreateProduct()
Creates a Product