public void Cannot_Delete_Nonexistent_Activity() { // Arrange - creating an activity var activity = new TimerActivity { Name = "Potato", ID = 2 }; // Arrange - create the mock repository var mock = new Mock <IRepository <TimerActivity> >(); mock.Setup(m => m.GetAll()).Returns(new[] { new TimerActivity { ID = 1, Name = "P1" }, new TimerActivity { ID = 3, Name = "P3" }, }.AsQueryable()); // Arrange - create a controller var target = new TimerController(mock.Object, null, null); // Act - delete the product target.DeleteActivity(activity.ID); // Assert - ensure that the repository delete method was // called with the correct Product mock.Verify(m => m.Delete(activity.ID)); Assert.Equal(2, mock.Object.GetAll().Count()); }
public void Cannot_Add_Log_With_Nonexisting_Activity() { // Arrange - create the mock repositories var mockRepositoryLog = new Mock <IRepository <TimerLog> >(); var mockRepositoryActivity = new Mock <IRepository <TimerActivity> >(); var activity = new TimerActivity { ID = 1, Name = "Sport" }; mockRepositoryActivity.Setup(m => m.GetAll()).Returns(new TimerActivity[] { }.AsQueryable); // Arrange - create a controller var target = new TimerController(mockRepositoryActivity.Object, null, mockRepositoryLog.Object); // action var result = target.AddLog(new AddLogViewModel { ActivityId = activity.ID }); // Assert mockRepositoryLog.Verify(m => m.Save(It.IsAny <TimerLog>()), Times.Never); // Assert - check the method result type Assert.IsType <NotFoundResult>(result); }
public void Can_Save_Valid_Activity() { // Arrange - create the mock repository var mock = new Mock <IRepository <TimerActivity> >(); // Arrange - create a controller var target = new TimerController(mock.Object, null, null); var activity = new TimerActivity { Name = "Potato" }; // Act - try to save the product var result = target.EditActivity(activity); // Assert - check that the repository was called mock.Verify(m => m.Save(activity)); // Assert - check the result type is a redirection Assert.IsType <RedirectToActionResult>(result); Assert.Equal("EditActivities", (result as RedirectToActionResult)?.ActionName); }
public void Cannot_Save_Invalid_Activity() { // Arrange - create the mock repository var mock = new Mock <IRepository <TimerActivity> >(); // Arrange - create a controller var target = new TimerController(mock.Object, null, null); var activity = new TimerActivity { Name = "Potato" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the product var result = target.EditActivity(activity); // Assert - check that the repository was not called mock.Verify(m => m.Save(It.IsAny <TimerActivity>()), Times.Never()); // Assert - check the method result type Assert.IsType <ViewResult>(result); }