using System; using System.Collections.Generic; using System.Threading.Tasks; using System.Web.Http.Results; using Microsoft.VisualStudio.TestTools.UnitTesting; using StoreApp.Controllers; using StoreApp.Models; namespace StoreApp.Tests { [TestClass] public class TestSimpleProductController { [TestMethod] public void GetAllProducts_ShouldReturnAllProducts() { var testProducts = GetTestProducts(); var controller = new SimpleProductController(testProducts); var result = controller.GetAllProducts() as List; Assert.AreEqual(testProducts.Count, result.Count); } [TestMethod] public async Task GetAllProductsAsync_ShouldReturnAllProducts() { var testProducts = GetTestProducts(); var controller = new SimpleProductController(testProducts); var result = await controller.GetAllProductsAsync() as List; Assert.AreEqual(testProducts.Count, result.Count); } [TestMethod] public void GetProduct_ShouldReturnCorrectProduct() { var testProducts = GetTestProducts(); var controller = new SimpleProductController(testProducts); var result = controller.GetProduct(4) as OkNegotiatedContentResult; Assert.IsNotNull(result); Assert.AreEqual(testProducts[3].Name, result.Content.Name); } [TestMethod] public async Task GetProductAsync_ShouldReturnCorrectProduct() { var testProducts = GetTestProducts(); var controller = new SimpleProductController(testProducts); var result = await controller.GetProductAsync(4) as OkNegotiatedContentResult; Assert.IsNotNull(result); Assert.AreEqual(testProducts[3].Name, result.Content.Name); } [TestMethod] public void GetProduct_ShouldNotFindProduct() { var controller = new SimpleProductController(GetTestProducts()); var result = controller.GetProduct(999); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); } private List GetTestProducts() { var testProducts = new List(); testProducts.Add(new Product { Id = 1, Name = "Demo1", Price = 1 }); testProducts.Add(new Product { Id = 2, Name = "Demo2", Price = 3.75M }); testProducts.Add(new Product { Id = 3, Name = "Demo3", Price = 16.99M }); testProducts.Add(new Product { Id = 4, Name = "Demo4", Price = 11.00M }); return testProducts; } } }