public void Create_POST_Does_Not_Save_Changes_If_ModelState_Is_Not_Valid() { // Arrange var controller = new TrackController(); var repo = new Mock<TestDB>(); repo.Setup(r => r.SaveChanges()).Throws<Exception>(); controller._repo = repo.Object; controller.ModelState.AddModelError("testError", new Exception()); // Act controller.Create(new TrackModel()); // Assert }
public void Create_POST_Redirects_Same_Model_To_GET_If_ModelState_Not_Valid() { // Arrange var controller = new TrackController(); var track = new TrackModel { Name = "One", Artist = "Metallica" }; var repo = new Mock<TestDB>(); controller._repo = repo.Object; controller.ModelState.AddModelError("testError", new Exception()); // Act var result = controller.Create(track) as ViewResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("", result.ViewName); Assert.AreEqual(track, result.Model); }
public void Create_POST_Saves_Track_If_ModelState_Is_Valid() { // Arrange var controller = new TrackController(); var track = new TrackModel { Name = "One", Artist = "Metallica" }; var repo = new Mock<TestDB>(); controller._repo = repo.Object; // TODO: Mock data mappers controller._m2tMapper = new SpotiChelas.DataMappers.TrackModelToTrackMapper(); // Act var result = controller.Create(track) as RedirectToRouteResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("action", result.RouteValues.First().Key); Assert.AreEqual("Index", result.RouteValues.First().Value); Assert.AreEqual(1, repo.Object.Tracks.Count()); repo.Verify(x => x.SaveChanges()); }