public void TestDeleteSportReturnsToIndexMethod() { //Arrange var sports = new[] { new Sport { Name = "Voetbal", SportId = 1 }, new Sport { Name = "Basketbal", SportId = 2 }, new Sport { Name = "Hockey", SportId = 3 }, new Sport { Name = "Volleybal", SportId = 4 }, }; var mockManager = new Mock <ISportManager>(); var mockTempData = new Mock <ITempDataDictionary>(); var controller = new SportController(mockManager.Object) { TempData = mockTempData.Object }; mockManager.Setup(x => x.DeleteSport(It.IsAny <Sport>())).Throws <Exception>(); mockManager.Setup(x => x.Read(It.IsAny <int>())).Returns(sports[1]); //Act var result = controller.Delete(sports[1].SportId); //Assert Assert.IsType(typeof(RedirectToActionResult), result); }
public async void TestDeleteSport() { //Arrange var serviceMock = new Mock <ISportService>(); var sport2Delete = new Sport { Id = 1, Name = "Sport" }; serviceMock.Setup(x => x.Remove(sport2Delete.Id)).Returns(Task.FromResult(result: sport2Delete.Id)); serviceMock.Setup(y => y.GetSportByIdAsync(sport2Delete.Id)). Returns(Task.FromResult(sport2Delete)); var controller = new SportController(serviceMock.Object); //Act var result = await controller.Delete(1) as ViewResult; var model = (Sport)result.ViewData.Model; //Assert Assert.Equal(expected: 1, actual: model.Id); }
public void Delete_Valid_Test() { // Arrange var mockRepository = new Mock <ISportRepository>(); mockRepository.Setup(x => x.DeleteSport(1)) .Returns(true); var controller = new SportController(mockRepository.Object); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act HttpResponseMessage response = controller.Delete(1); // Assert string responseJSONStr = response.Content.ReadAsStringAsync().Result; string responseStr = Helper.Deserialize <string>(responseJSONStr); Assert.IsNotNull(response); Assert.AreEqual(response.StatusCode, HttpStatusCode.OK); Assert.AreEqual(responseStr, "Sport successfully deleted."); }