public async Task TestCreate() { cleanUpDir = @"..\..\..\..\MobilePhones.Tests\testfiles\images\phone_images\"; cleanUpSearchPattern = "*.jpg"; int beforeCount = _context.Phones.Count(); var file = new Mock <IFormFile>(); var fileName = "test.jpg"; file.Setup(f => f.FileName).Returns(fileName).Verifiable(); _hostEnv.Setup(h => h.WebRootPath).Returns(@"..\..\..\..\MobilePhones.Tests\testfiles\").Verifiable(); var phone = new Phone() { Name = "My Phone", Company = "Company", Price = 444, ImageFile = file.Object }; var result = await _controller.Create(phone); Assert.Contains(fileName, phone.ImageName); Assert.Equal(beforeCount + 1, _context.Phones.Count()); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", redirectToActionResult.ActionName); }
public async Task TestCreate() { var options = new DbContextOptionsBuilder <MobileContext>() .UseInMemoryDatabase(databaseName: "phonesdb") .Options; using (var context = new MobileContext(options)) { int beforeCount = context.Phones.Count(); var phone = new Phone { Name = "SuperPhone", Company = "KyrgyzCompany", Price = 20 }; _phonesController = new PhonesController(context, _ihost); var result = await _phonesController.Create(phone); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", redirectToActionResult.ActionName); Assert.Equal(beforeCount + 1, context.Phones.Count()); } }