public void GivenABlogPost_WhenDelete_ReturnResult() { var deleteData = new BlogPost { PostId = 1, Category = new BlogCategory { CategoryId = 1, Name = "Category Alpha" }, Creator = "User One", Title = "My first post", Body = "Hello!", Dt = DateTime.Now }; var loggerMock = new Mock <ILogger <BlogPostController> >(); var postServiceMock = new Mock <IBlogPostService>(); postServiceMock.Setup(p => p.Delete(It.IsAny <BlogPost>())); postServiceMock.Setup(p => p.Get(1)).Returns(deleteData); var categoryServiceMock = new Mock <IBlogCategoryService>(); var controller = new BlogPostController(loggerMock.Object, postServiceMock.Object, categoryServiceMock.Object); var result = controller.DeleteBlogPost(1); Assert.NotNull(result); var actionResult = Assert.IsType <ActionResult <BlogPostViewModel> >(result); var returnedDelete = Assert.IsType <BlogPostViewModel>(actionResult.Value); Assert.Equal(1, returnedDelete.PostId); Assert.Equal("My first post", returnedDelete.Title); Assert.Equal("Hello!", returnedDelete.Body); }
public async Task DeleteBlogPostReturnsNotFound() { // Arrange var mockRepository = new Mock <IBlogPostService>(); mockRepository.Setup(s => s.DeleteBlogPost("augmented_reality_ios_application")) .ThrowsAsync(new ArgumentException()); var controller = new BlogPostController(mockRepository.Object); // Act IHttpActionResult actionResult = await controller.DeleteBlogPost("augmented_reality_ios_application"); // Assert Assert.IsNotNull(actionResult); Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult)); }
public async Task DeleteBlogPostReturnsOkResponse() { // Arrange var mockRepository = new Mock <IBlogPostService>(); mockRepository.Setup(s => s.DeleteBlogPost("augmented_reality_ios_application")) .Returns(Task.FromResult(default(object))); var controller = new BlogPostController(mockRepository.Object); // Act IHttpActionResult actionResult = await controller.DeleteBlogPost("augmented_reality_ios_application"); // Assert Assert.IsNotNull(actionResult); Assert.IsInstanceOfType(actionResult, typeof(OkResult)); }
public async Task Given_IdDoesExist_When_Delete_Then_DeleteAndReturnBlogPost() { var options = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(databaseName: "DeleteSuccess") .Options; using (var context = new Context(options)) { context.BlogPosts.AddRange(blogPosts); context.SaveChanges(); } using (var context = new Context(options)) { var service = new BlogPostController(new GenericRepository <BlogPost>(context)); var result = await service.DeleteBlogPost(blogPosts[0].Id); Assert.AreEqual(1, await context.BlogPosts.CountAsync()); } }
public async Task Given_IdDoesNotExist_When_Delete_Then_Return404() { var options = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(databaseName: "DeleteFail") .Options; using (var context = new Context(options)) { context.BlogPosts.AddRange(blogPosts); context.SaveChanges(); } using (var context = new Context(options)) { var service = new BlogPostController(new GenericRepository <BlogPost>(context)); var result = await service.DeleteBlogPost(new Guid()); Assert.AreEqual(404, ((result as ActionResult <BlogPost>).Result as StatusCodeResult).StatusCode); } }