public async Task Cannot_Save_Invalid_Sound() { // Arrange - create the mock repository var mockRepository = new Mock <IRepository <TimerSound> >(); // Arrange - create a controller var targetController = new TimerController(null, mockRepository.Object, null); var mockFile = new Mock <IFormFile>(); mockFile.Setup(x => x.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); var model = new EditSoundViewModel { Name = "Potato", File = mockFile.Object }; // Arrange - add an error to the model state targetController.ModelState.AddModelError("error", "error"); // Act - try to save the product var result = await targetController.EditSound(model); // Assert - check that the repository was not called mockRepository.Verify(m => m.Save(It.IsAny <TimerSound>()), Times.Never()); // Assert - check the method result type Assert.IsType <ViewResult>(result); }
public async Task Can_Save_Valid_Sound() { // Arrange - create the mock repository var mockRepository = new Mock <IRepository <TimerSound> >(); // Arrange - create a controller var targetController = new TimerController(null, mockRepository.Object, null); var mockFile = new Mock <IFormFile>(); mockFile.Setup(x => x.CopyToAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); var model = new EditSoundViewModel { Name = "Potato", File = mockFile.Object }; // Act - try to save the sound var result = await targetController.EditSound(model); // Assert - check that the repository was called mockRepository.Verify(m => m.Save(It.IsAny <TimerSound>())); // Assert - check the result type is a redirection Assert.IsType <RedirectToActionResult>(result); Assert.Equal("EditSounds", (result as RedirectToActionResult)?.ActionName); }