protected void btnDelete_Click(object sender, EventArgs e) { try { NewsController db = new NewsController(); if (Request.QueryString["id"] != null) { int id = Convert.ToInt32(Request.QueryString["id"]); db.Delete(id); db.DeleteNewsGroupNews(id); string url = DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "admin_news", "mid/" + this.ModuleId.ToString(), "cat/" + lblCatString.Text); Response.Redirect(url); } else if (lblNewsID.Text != "") { int id = Convert.ToInt32(lblNewsID.Text); db.Delete(id); db.DeleteNewsGroupNews(id); string url = DotNetNuke.Common.Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "admin_news", "mid/" + this.ModuleId.ToString(), "cat/" + lblCatString.Text); Response.Redirect(url); } } catch (Exception ex) { Response.Write(ex.Message); } }
public void DeleteTest() { // Act controller.db.News.Add(new News { ID = 1, Date = DateTime.Now, Text = "txt", Title = "title" }); ViewResult result = controller.Delete(1) as ViewResult; // Assert Assert.NotNull(result); }
public void Delete_Not_Ajax_Request() { // Arrange var id = 1; var news = new News() { ID = 1, AddedDate = DateTime.Now, Position = 1, Title = "New 1", Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa." }; var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var fakeController = new FakeController(); var controller = new NewsController(service, mapper); fakeController.PrepareFakeRequest(); controller.ControllerContext = fakeController.GetControllerContext <NewsController>(new RouteData(), controller); service.Get(id).Returns(news); // Act var result = controller.Delete(id) as RedirectToRouteResult; var ajaxRequest = controller.Request.IsAjaxRequest(); var actionName = result.RouteValues.Values.ElementAt(0); // Assert Assert.That(result, !Is.Null); Assert.That(ajaxRequest, Is.False); Assert.That("Index", Is.EqualTo(actionName)); }
public void Good_Delete() { // Arrange var id = 1; var news = new News() { ID = 1, AddedDate = DateTime.Now, Position = 1, Title = "New 1", Content = "Quisque nulla nunc, tempor eu lorem non, pharetra laoreet massa." }; var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var fakeController = new FakeController(); var controller = new NewsController(service, mapper); fakeController.PrepareFakeAjaxRequest(); controller.ControllerContext = fakeController.GetControllerContext <NewsController>(new RouteData(), controller); service.Get(id).Returns(news); service.Delete(news); service.Save(); // Act var result = controller.Delete(id) as JsonResult; var ajaxRequest = controller.Request.IsAjaxRequest(); var jsonRequestBehavior = result.JsonRequestBehavior; var data = result.Data; // Assert Assert.That(result, !Is.Null); Assert.That(ajaxRequest, Is.True); Assert.That(JsonRequestBehavior.AllowGet, Is.EqualTo(jsonRequestBehavior)); Assert.That("", Is.EqualTo(data)); }
public async Task DeleteShouldReturnNotFound() { string userId = "Jack"; var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim("userId", userId) }, "mock")); int newsId = 103; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.DeleteNews(userId, newsId)).Throws(new NoNewsFoundException($"NewsId {newsId} for {userId} doesn't exist")); var controller = new NewsController(mockService.Object); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = user } }; var actual = await controller.Delete(newsId); var actionResult = Assert.IsType <NotFoundObjectResult>(actual); Assert.Equal($"NewsId {newsId} for {userId} doesn't exist", actionResult.Value); }
public async Task DeleteShouldReturnOk() { string userId = "Jack"; var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim("userId", userId) }, "mock")); int newsId = 102; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.DeleteNews(userId, newsId)).Returns(Task.FromResult(true)); var controller = new NewsController(mockService.Object); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = user } }; var actual = await controller.Delete(newsId); var actionResult = Assert.IsType <OkObjectResult>(actual); Assert.True(Convert.ToBoolean(actionResult.Value)); }
public void DeleteFavoriteNews_BadRequest_Test() { mockNewsService.Setup(fk => fk.DeleteFavoriteNews(It.IsAny <int>())); var newsController = new NewsController(this.mockNewsService.Object); var result = newsController.Delete(0) as StatusCodeResult; Assert.Equal((int)HttpStatusCode.BadRequest, result.StatusCode); }
public void Delete() { // Arrange NewsController controller = new NewsController(); // Act controller.Delete(5); // Assert }
protected void Delete_Click(object sender, EventArgs e) { LinkButton b = (LinkButton)sender; int NewsId = int.Parse(b.CommandArgument); NewsController ns = new NewsController(); ns.Delete(NewsId); rptNews.DataSource = ns.GetNews(); rptNews.DataBind(); }
public void DeleteFavoriteNews_DBException_Test() { mockNewsService.Setup(fk => fk.DeleteFavoriteNews(It.IsAny <int>())).Throws(new Exception()); var newsController = new NewsController(this.mockNewsService.Object); var result = newsController.Delete(1) as ObjectResult; var errorMessage = Assert.IsType <string>(result.Value); Assert.Equal("Failed to Remove News from Favorite", errorMessage); Assert.Equal((int)HttpStatusCode.InternalServerError, result.StatusCode); }
public void NewsControllerDeleteNonExistingNewsData_ReturnedBadStatusCode() { //Arrenge var db = this.Context; var newsController = new NewsController(db); //Act var testDataCount = this.GetTestData().Count(); var testCode = newsController.Delete(testDataCount + 1); Assert.IsType <BadRequestResult>(testCode); }
private void DeleteNewsFails(HttpStatusCode code) { var newsController = new NewsController(this.newsStorageMock.Object, this.inputValidation.Object, this.businessValidation.Object); newsController.ConfigureForTesting(HttpMethod.Delete, string.Format("{0}/{1}/{2}/{3}", NewsUriRoot, City, Date, Id)); newsController.User = new GenericPrincipal(new GenericIdentity(DefaultUser.Nickname), null); HttpResponseMessage response = newsController.Delete(City, Date, Id.ToString()).Result; Assert.AreEqual(code, response.StatusCode); this.newsStorageMock.Verify(storage => storage.DeleteNews(City, DateTime.Parse(Date), Id), Times.Never()); }
public void DeleteNews_ShouldReturn_NotFoundResult_ForInValidId() { var mockINewsService = new Mock <INewsService>(); mockINewsService.Setup(x => x.DeleteNews(It.IsAny <int>())).Throws <ArgumentException>(); var newsController = new NewsController(mockINewsService.Object); // Act var actual = newsController.Delete(6); //Assert var result = Assert.IsType <NotFoundObjectResult>(actual); }
public async Task DeleteShouldReturnNotFound() { int newsId = 103; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.RemoveNews(newsId)).Throws(new NewsNotFoundException($"No news found with Id: {newsId}")); var controller = new NewsController(mockService.Object); var actual = await Assert.ThrowsAsync <NewsNotFoundException>(() => controller.Delete(newsId)); Assert.Equal($"No news found with Id: {newsId}", actual.Message); }
public async Task Delete_Should_RemoveItemfromList() { var mockRepo = new Mock <IDataRepository <NewsArticle, long> >(MockBehavior.Strict); var mocklogger = new Mock <ILogger <NewsController> >(); mockRepo.Setup(repo => repo.Delete(1)).ReturnsAsync(true); var controller = new NewsController(mockRepo.Object, mocklogger.Object); var result = await controller.Delete(1); Assert.IsNotNull(result); Assert.IsInstanceOf <NoContentResult>(result); }
public void DeleteForInVaildId() { var mockINewsService = new Mock <INewsService>(); mockINewsService.Setup(x => x.Delete(It.IsAny <int>())).Returns(false); var newsController = new NewsController(mockINewsService.Object); // Act var result = newsController.Delete(6) as NotFoundObjectResult; //Assert Assert.NotNull(result); Assert.Equal(404, result.StatusCode); }
public async Task DeleteShouldReturnOk() { int newsId = 103; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.RemoveNews(newsId)).Returns(Task.FromResult(true)); var controller = new NewsController(mockService.Object); var actual = await controller.Delete(newsId); var actionResult = Assert.IsType <OkObjectResult>(actual); Assert.True(Convert.ToBoolean(actionResult.Value)); }
public void NewsControllerDeleteExistingNewsData_ReturnedOkStatusCode() { //Arrenge var db = this.Context; this.PopulateData(db); var newsController = new NewsController(db); //Act var testModel = this.GetTestData().First(); var testCode = newsController.Delete(1); Assert.IsType <OkResult>(testCode); }
public void DeleteFavoriteNews_Test() { var mockNews = getMockNews(); mockNewsService.Setup(fk => fk.DeleteFavoriteNews(It.IsAny <int>())); var newsController = new NewsController(this.mockNewsService.Object); var result = newsController.Delete(1) as ObjectResult; var postedNews = Assert.IsType <bool>(result.Value); Assert.Equal((int)HttpStatusCode.OK, result.StatusCode); Assert.True(postedNews); }
public async Task DeleteShouldReturnNotFound() { int newsId = 103; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.RemoveNews(newsId)).Throws(new NewsNotFoundException($"No news found with Id: {newsId}")); var controller = new NewsController(mockService.Object); var actual = await controller.Delete(newsId); var actionResult = Assert.IsType <NotFoundObjectResult>(actual); Assert.Equal($"No news found with Id: {newsId}", actionResult.Value); }
public void DeleteNews_ShouldReturn_OkObjectResult_ForValidId() { var mockINewsService = new Mock <INewsService>(); mockINewsService.Setup(service => service.DeleteNews(3)); var newsController = new NewsController(mockINewsService.Object); // Act var actual = newsController.Delete(3); //Assert Assert.IsType <OkResult>(actual); }
public void TestDeleteNewsReturns204NoContentWhenDeletesUser() { this.businessValidation.Setup(validation => validation.IsValidGetNews(this.newsStorageMock.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>())).Returns(Task.FromResult(true)); this.businessValidation.Setup(validation => validation.IsValidModifyNews(this.newsStorageMock.Object, It.IsAny <string>(), It.IsAny <DateTime>(), It.IsAny <Guid>(), It.IsAny <string>())).Returns(Task.FromResult(true)); var newsController = new NewsController(this.newsStorageMock.Object, this.inputValidation.Object, this.businessValidation.Object); newsController.ConfigureForTesting(HttpMethod.Delete, string.Format("{0}/{1}/{2}/{3}", NewsUriRoot, City, Date, Id)); newsController.User = new GenericPrincipal(new GenericIdentity(DefaultUser.Nickname), null); HttpResponseMessage response = newsController.Delete(City, Date, Id.ToString()).Result; Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); this.newsStorageMock.Verify(storage => storage.DeleteNews(City, DateTime.Parse(Date), Id), Times.Once()); }
public async Task DeleteShouldReturnNotFound() { string userId = "Jack"; int newsId = 103; var mockService = new Mock <INewsService>(); mockService.Setup(svc => svc.DeleteNews(userId, newsId)).Throws(new NoNewsFoundException($"NewsId {newsId} for {userId} doesn't exist")); var controller = new NewsController(mockService.Object); var actual = await controller.Delete(userId, newsId); var actionResult = Assert.IsType <NotFoundObjectResult>(actual); Assert.Equal($"NewsId {newsId} for {userId} doesn't exist", actionResult.Value); }
public async Task DeleteShouldReturnToIndex() { int newsId = 101; var mockRepo = new Mock <INewsRepository>(); mockRepo.Setup(repo => repo.RemoveNews(newsId)).Returns(Task.FromResult(true)); var newsController = new NewsController(mockRepo.Object); var actual = await newsController.Delete(newsId); var actionResult = Assert.IsType <RedirectToActionResult>(actual); Assert.Null(actionResult.ControllerName); Assert.Equal("Index", actionResult.ActionName); }
public void Delete_Id_Is_Null() { // Arrange int?id = null; var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new NewsController(service, mapper); // Act var result = controller.Delete(id) as HttpStatusCodeResult; var statusCode = result.StatusCode; // Assert Assert.That(result, !Is.Null); Assert.That(400, Is.EqualTo(statusCode)); }
public void DeleteNewsTest() { var mockINewsService = new Mock <INewsService>(); mockINewsService.Setup(service => service.Delete(3)).Returns(true); var newsController = new NewsController(mockINewsService.Object); // Act var result = newsController.Delete(3) as OkObjectResult; var actualResult = (bool)result.Value; //Assert Assert.NotNull(result); Assert.Equal(200, result.StatusCode); Assert.True(actualResult); }
public void NewsControllerDeleteShouldReturnOkStatus() { // Arrange Mapper.Initialize(config => config.AddProfile <AutoMapperProfile>()); var db = this.Context; this.PopulateData(db); var newsService = new NewsService(db); var newsController = new NewsController(newsService); // Act var result = newsController.Delete(1); // Assert Assert.IsType <OkObjectResult>(result); }
public void NewsControllerDeleteNonExistingNewsShouldReturnBadRequest() { // Arrange Mapper.Initialize(config => config.AddProfile <AutoMapperProfile>()); var db = this.Context; this.PopulateData(db); var newsService = new NewsService(db); var newsController = new NewsController(newsService); // Act var result = newsController.Delete(4); // Assert Assert.IsType <BadRequestResult>(result); }
public void Delete_Model_Null() { // Arrange var id = -1; News news = null; var service = Substitute.For <INewsRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new NewsController(service, mapper); service.Get(id).Returns(news); // Act var result = controller.Delete(id) as HttpNotFoundResult; var statusCode = result.StatusCode; // Assert Assert.That(result, !Is.Null); Assert.That(404, Is.EqualTo(statusCode)); }
public void DeleteItemWithNonExistingIdReturnsBadRequest(int id) { //Arrange TestUtils testUtils = new TestUtils(); TestUtils.InitializeMapper(); var db = testUtils.GetDbContext(); this.PopulateDb(db); INewsService newsService = new NewsService(db); var sut = new NewsController(newsService); //Act var result = sut.Delete(id); //Assert Assert.IsType <BadRequestObjectResult>(result); }
public void Delete_ShouldReturn400BadRequestOnNonExistingItem() { var news = this.mocks.NewsRepositoryMock.Object.All().ToList(); int nonExistingNewsId = 5; this.mocks.NewsRepositoryMock .Setup(r => r.Delete(It.IsAny<NewsModel>())) .Callback((NewsModel newsModel) => { news.Remove(newsModel); }); var mockContext = new Mock<INewsData>(); mockContext .Setup(c => c.News) .Returns(this.mocks.NewsRepositoryMock.Object); var newsController = new NewsController(mockContext.Object); this.SetupController(newsController); var response = newsController.Delete(nonExistingNewsId) .ExecuteAsync(CancellationToken.None).Result; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }