public async Task Respond_To_DELETE() { var result = await _controller.Delete(null); Assert.IsInstanceOf(typeof(JArray), result); Assert.IsEmpty(result); }
public async void CanRemoveToDoItemsFromAToDoList() { DbContextOptions <ToDoDbContext> options = new DbContextOptionsBuilder <ToDoDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (ToDoDbContext context = new ToDoDbContext(options)) { ToDoList list = new ToDoList { ID = 1, Name = "test list", IsDone = true }; ToDoListController tdlc = new ToDoListController(context); await tdlc.Create(list); ToDoList result = context.ToDoLists.Find(list.ID); Assert.Equal("test list", result.Name); ToDoItem item = new ToDoItem { ID = 1, Name = "test todo", IsDone = true, ListID = 1 }; ToDoItem item2 = new ToDoItem { ID = 2, Name = "to test do", IsDone = false, ListID = 1 }; ToDoController tdc = new ToDoController(context); await tdc.Create(item); await tdc.Create(item2); await tdc.Delete(item.ID); await tdc.Delete(item2.ID); var viewItems = tdlc.GetById(list.ID).Result; var r = (ObjectResult)viewItems.Result; ToDoList todoItems = (ToDoList)r.Value; Assert.Empty(todoItems.ToDoItems); } }
public void TestDeleteOK() { //SETUP var options = SqliteInMemory.CreateOptions <ExampleDbContext>(); using (var context = new ExampleDbContext(options)) { context.Database.EnsureCreated(); context.SeedDatabase(); var controller = new ToDoController(); var utData = context.SetupSingleDtoAndEntities <ChangeDifficultyDto>(_genericServiceConfig); var service = new CrudServices(context, utData.ConfigAndMapper); //ATTEMPT var response = controller.Delete(2, service); //VERIFY response.GetStatusCode().ShouldEqual(CreateResponse.OkStatusCode); var rStatus = response.CopyToStatus(); rStatus.IsValid.ShouldBeTrue(rStatus.GetAllErrors()); rStatus.Message.ShouldEqual("Successfully deleted a Todo Item"); context.TodoItems.Count().ShouldEqual(5); } }
public void CanDeleteAToDoItem() { DbContextOptions <ToDoDbContext> options = new DbContextOptionsBuilder <ToDoDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (ToDoDbContext context = new ToDoDbContext(options)) { ToDoItem item = new ToDoItem { ID = 1, Name = "test todo", IsDone = true }; ToDoController tdc = new ToDoController(context); var result = tdc.Create(item).Result; var results = context.ToDoItems.Where(i => i.Name == "test todo"); Assert.Equal(1, results.Count()); var remove = tdc.Delete(item.ID); Assert.True(remove.IsCompletedSuccessfully); } }
public void DeleteSuccessful() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete, }; //Actual var toDoItems = controller.Get(); var results = toDoItems.Content.ReadAsAsync <System.Collections.Generic.List <Domain.Models.ToDoItem> >().Result; var controllerDelete = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); controllerDelete.Request = new HttpRequestMessage { Method = HttpMethod.Delete, }; controllerDelete.Configuration = new HttpConfiguration(); var response = controllerDelete.Delete(results[results.Count - 1].Id); //Assert Assert.IsNotNull(response); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); }
public async void CanDeleteItem() { DbContextOptions <ToDoNgDbContext> options = new DbContextOptionsBuilder <ToDoNgDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; using (ToDoNgDbContext context = new ToDoNgDbContext(options)) { // Arrange ToDoController controller = new ToDoController(context); CreatedAtActionResult postResult = await controller.Post(new ToDo() { Message = "Test Item", IsDone = false, ListId = null }) as CreatedAtActionResult; int afterPostCount = await context.ToDo.CountAsync(); // Act await controller.Delete((postResult.Value as ToDo).Id); // Assert Assert.True(afterPostCount > (await context.ToDo.CountAsync())); } }
public async Task TestDeleteAccurateId() { // Arrange ToDoController controller = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); ToDoController controllerGet = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); // Act var result = await controllerGet.GetAll(); var data = result.Data.GetType().GetProperty("data"); var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null); if (dataVal != null && dataVal.Count > 0) { var item = dataVal[dataVal.Count - 1]; var resultDelete = await controller.Delete(item.Id); //Assert Assert.IsTrue(resultDelete.Data.ToString() == "", "Since the id is not present so throwing error."); } else { Assert.IsTrue(true); } }
public async Task delete_removes_corresponding_todo_from_inmemory_repository() { var todo = new Persistence.ToDo { Id = 100 }; await _dbContext.Database.EnsureDeletedAsync(); await _dbContext.Database.EnsureCreatedAsync(); _repository = new Repository <Persistence.ToDo>(_dbContext); _sutWithInMemory = new ToDoController(_repository); _sutWithInMemory.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "Test User"), new Claim(ClaimTypes.NameIdentifier, "*****@*****.**") })) } }; await _sutWithInMemory.Post(todo); await _sutWithInMemory.Delete(100); }
public async Task delete_removes_corresponding_todo_from_repository() { var todo = new Persistence.ToDo(); _mockRepository.Setup(x => x.RemoveAsync(todo)); await _sut.Delete(1); _mockRepository.Verify(); }
public async Task TestToDoDeleteException() { // Arrange ToDoController controller = new ToDoController(new ToDoMockService()); //Act var result = await controller.Delete("45"); //Assert Assert.IsTrue(result.Data.ToString().ToLower() == "error", "Exception occur so error."); }
public void DeleteShouldDeleteExistingItemAndRedirectRouteToIndex() { var mockRepository = new Mock <IToDoItemsRepository>(); var controller = new ToDoController(mockRepository.Object); var result = controller.Delete(5).Result as RedirectToRouteResult; mockRepository.Verify(repo => repo.DeleteItem(It.IsAny <int>()), Times.Once); Assert.AreEqual("Index", result.RouteValues.FirstOrDefault().Value); }
public async Task DeleteFourHundredResponseWhenIdIsNegative() { //Arange var toDoId = -1; var userId = 1242; //Act var actionResult = await _toDoController.Delete(toDoId, userId); //Assert Assert.IsInstanceOf <BadRequestResult>(actionResult); }
public void TestDeleteException() { mock.Expects.One.Method(_ => _.Find(45)).WithAnyArguments().Will(Throw.Exception(new Exception())); mock.Expects.One.Method(_ => _.Delete(new DataAccessLayer.Models.ToDoItem { Name = "sample" })).WithAnyArguments().WillReturn(1); var controller = new ToDoController(new ToDoBL(mock.MockObject, mockCache.MockObject)); var result = controller.Delete(45); // Assert Assert.IsTrue(result.Data.ToString() == "error", "Exception thrown"); }
public void TestDeleteWrongId() { mock.Expects.One.MethodWith(_ => _.Find(45)).WillReturn(null); mock.Expects.One.MethodWith(_ => _.Delete(new DataAccessLayer.Models.ToDoItem { Name = "sample" })).WillReturn(0); var controller = new ToDoController(new ToDoBL(mock.MockObject, mockCache.MockObject)); var result = controller.Delete(45); // Assert Assert.IsTrue(result.Data.ToString() == "error", "Since the id is not present so throwing error."); }
public async Task TestDeleteWrongId() { // Arrange ToDoController controller = new ToDoController(new ToDoBL(new ToDoRepository())); // Act controller.ModelState.AddModelError("test", "test"); var result = await controller.Delete("100"); //Assert Assert.IsTrue(result.Data.ToString().ToLower() == "error", "Since the id is not present so throwing error."); }
public void TestDeleteWrongId() { // Arrange ToDoController controller = new ToDoController(new ToDoBL(new ToDoRepository())); // Act var result = (RedirectToRouteResult)controller.Delete("45").Result; // Assert Assert.IsNotNull(result, "Not a redirect result"); Assert.IsFalse(result.Permanent); // Or IsTrue if you use RedirectToActionPermanent Assert.AreEqual("Index", result.RouteValues["Action"]); Assert.AreEqual("Error", result.RouteValues["Controller"]); }
public void DeleteToDo_UnitTest() { var toDoController = new ToDoController(_mockContext.Object); var result = toDoController.Delete(1); var deleteResult = result as OkResult; Assert.IsNotNull(deleteResult); Assert.AreEqual(200, deleteResult.StatusCode); _toDoRepository.Verify(m => m.Delete(It.IsAny <int>()), Times.Once); _mockContext.Verify(m => m.Save(), Times.Once); }
public void TestToDoDeleteException() { // Arrange using (var mock = new ToDoMockService()) { using (ToDoController controller = new ToDoController(mock)) { //Act var result = controller.Delete(45); //Assert Assert.IsTrue(result.Data.ToString() == "error", "Exception occur so error."); } } }
public void TestDeleteAccurateId() { mock.Expects.One.Method(_ => _.Find(45)).WithAnyArguments().WillReturn(new DataAccessLayer.Models.ToDoItem { Id = 45, Name = "sample" }); mock.Expects.One.Method(_ => _.Delete(new DataAccessLayer.Models.ToDoItem { Name = "sample" })).WithAnyArguments().WillReturn(1); var controller = new ToDoController(new ToDoBL(mock.MockObject, mockCache.MockObject)); var result = controller.Delete(45); // Assert Assert.IsTrue(true); }
public void TestToDoDeleteException() { // Arrange using (ToDoController controller = new ToDoController(new ToDoMockService())) { // Act var result = (RedirectToRouteResult)controller.Delete("45").Result; // Assert Assert.IsNotNull(result, "Not a redirect result"); Assert.IsFalse(result.Permanent); // Or IsTrue if you use RedirectToActionPermanent Assert.AreEqual("Index", result.RouteValues["Action"]); Assert.AreEqual("Error", result.RouteValues["Controller"]); } }
public void DeleteShouldReturnErrorForInvalidItemId() { var mockRepository = new Mock <IToDoItemsRepository>(); var controller = new ToDoController(mockRepository.Object); var actionResult = controller.Delete(null).Result; var viewResult = actionResult as PartialViewResult; Assert.IsNull(viewResult); var httpResult = actionResult as HttpStatusCodeResult; Assert.AreEqual((int)HttpStatusCode.BadRequest, httpResult.StatusCode); }
public void TestDeleteAccurateId() { // Arrange ToDoController controller = new ToDoController(new ToDoBL(new ToDoRepository())); ToDoController controllerDelete = new ToDoController(new ToDoBL(new ToDoRepository())); // Act ViewResult resultGetAll = controllerDelete.Index().Result as ViewResult; ToDoList model = (ToDoList)resultGetAll.Model; //Please check the below id is correct otherwise this test case fail var result = (RedirectToRouteResult)controller.Delete(model.Items[model.Items.Count - 1].Id).Result; // Assert Assert.IsNotNull(result, "Not a redirect result"); Assert.IsFalse(result.Permanent); // Or IsTrue if you use RedirectToActionPermanent Assert.AreEqual("Index", result.RouteValues["Action"]); }
public async Task DeleteNoContentResponse() { //Arrange var mockService = new Mock <IToDoService>(); mockService.Setup( service => service.Delete(It.IsAny <int>(), It.IsAny <int>()) ).ReturnsAsync(true); var controller = new ToDoController(mockService.Object, _mockLogger, _mockCache); //Act var result = await controller.Delete(_toDoId, _userId); //Assert Assert.IsNotNull(result); Assert.IsInstanceOf <NoContentResult>(result); }
public async Task DeleteException() { //Arrange var controller = new ToDoController(new ToDoMockService()); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete }; //Actual string id = "0"; var response = await controller.Delete(id); //Assert Assert.IsNotNull(response); Assert.IsNull(response.Content); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public void DeleteListVerify() { int listId = 1; var boardMock = new Mock <IStore <Board> >(); var listMock = new Mock <IStore <List> >(); listMock.Setup(store => store.Delete(listId)); var eventMock = new Mock <IStore <Event> >(); var userStoreMock = new Mock <IUserStore <ApplicationUser> >(); var userMock = new Mock <UserManager <ApplicationUser> >( userStoreMock.Object, null, null, null, null, null, null, null, null); ToDoController controller = new ToDoController(listMock.Object, userMock.Object, eventMock.Object, boardMock.Object); controller.Delete(listId); listMock.Verify(store => store.Delete(listId)); }
public void TestDeleteWrongId() { // Arrange using (var repository = new ToDoRepository()) { using (var businessLogic = new ToDoBL(repository)) { using (ToDoController controller = new ToDoController(businessLogic)) { // Act controller.ModelState.AddModelError("test", "test"); var result = controller.Delete(45); //Assert Assert.IsTrue(result.Data.ToString() == "error", "Since the id is not present so throwing error."); } } } }
public async Task DeleteFailure() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete }; //Actual string id = "0"; var response = await controller.Delete(id); //Assert Assert.IsNotNull(response); Assert.IsNull(response.Content); Assert.IsFalse(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); }
public void DeleteFailure() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository(), new DataCache())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete, }; //Actual int id = 0; var response = controller.Delete(id); //Assert Assert.IsNotNull(response); Assert.IsNull(response.Content); Assert.IsFalse(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode); }
public void TestDeleteAccurateId() { // Arrange using (var repository = new ToDoRepository()) { using (var businessLogic = new ToDoBL(repository, new DataCache())) { using (ToDoController controller = new ToDoController(businessLogic)) { using (var repositoryGet = new ToDoRepository()) { using (var businessLogicGet = new ToDoBL(repositoryGet, new DataCache())) { using (ToDoController controllerGet = new ToDoController(businessLogicGet)) { // Act var result = controllerGet.GetAll(); var data = result.Data.GetType().GetProperty("data"); var dataVal = (List <ToDoItem>)data.GetValue(result.Data, null); if (dataVal != null && dataVal.Count > 0) { var item = dataVal[dataVal.Count - 1]; var resultDelete = controller.Delete(item.Id); //Assert Assert.IsTrue(String.IsNullOrEmpty(resultDelete.Data.ToString()), "Since the id is not present so throwing error."); } else { Assert.IsTrue(true); } } } } } } } }
public async Task DeleteSuccessful() { //Arrange var controller = new ToDoController(new ToDoBL(new ToDoRepository())); controller.Request = new HttpRequestMessage { Method = HttpMethod.Delete }; controller.Request.Headers.Add("ZUMO-API-VERSION", "2.0.0"); controller.Configuration = new HttpConfiguration(); controller.Configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); controller.RequestContext.RouteData = new HttpRouteData( route: new HttpRoute(), values: new HttpRouteValueDictionary { { "controller", "ToDo" } }); //Actual var toDoItems = await controller.Get(); var results = toDoItems.Content.ReadAsAsync <System.Collections.Generic.List <Domain.Models.ToDoItem> >().Result; if (results != null && results.Count > 0) { var response = controller.Delete(results[results.Count - 1].Id).Result; //Assert Assert.IsNotNull(response); Assert.IsTrue(response.IsSuccessStatusCode); Assert.AreEqual(HttpStatusCode.Accepted, response.StatusCode); } else { Assert.IsTrue(true); } }