public async Task GetBlogPostReturnsOkResponse() { // Arrange BlogPost blogPost = new BlogPost { Slug = "augmented_reality_iOS_application", Title = "Augmented Reality iOS Application", Description = "Rubicon Software Development and Gazzda furniture are proud to launch an augmented reality app.", Body = "The app is simple to use, and will help you decide on your best furniture fit.", TagList = new string[] { "iOS", "AR" } }; var mockRepository = new Mock <IBlogPostService>(); mockRepository.Setup(s => s.GetBlogPostBySlug(blogPost.Slug)) .Returns(Task.FromResult(blogPost)); var controller = new BlogPostController(mockRepository.Object); // Act IHttpActionResult actionResult = await controller.GetBlogPost(blogPost.Slug); var createdResult = actionResult as OkNegotiatedContentResult <BlogPost>; // Assert Assert.IsNotNull(actionResult); Assert.IsNotNull(createdResult.Content); Assert.IsInstanceOfType(createdResult.Content, typeof(BlogPost)); Assert.AreEqual("augmented_reality_iOS_application", blogPost.Slug); }
public async Task GetBlogPostReturnsOkNoResponseBody() { // Arrange var mockRepository = new Mock <IBlogPostService>(); var controller = new BlogPostController(mockRepository.Object); // Act IHttpActionResult actionResult = await controller.GetBlogPost("internet_trends_2018"); var createdResult = actionResult as OkNegotiatedContentResult <BlogPost>; // Assert Assert.IsNotNull(actionResult); Assert.IsNull(createdResult.Content); }
public void If_BlogPostRepository_Returns_Null_Then_Return_NotFoundResult() { var fixture = new Fixture(); var id = fixture.Create <int>(); var mockBlogPostRepository = new Mock <IBlogPostRepository>(); mockBlogPostRepository .Setup(x => x.Get(id)) .Returns <BlogPost>(null); var sut = new BlogPostController(mockBlogPostRepository.Object); var actual = sut.GetBlogPost(id); Assert.That(actual, Is.TypeOf <NotFoundResult>()); }
public async void WhenGetBlogPost_AndNotExists_ReturnNull() { var loggerMock = new Mock <ILogger <BlogPostController> >(); var postServiceMock = new Mock <IBlogPostService>(); postServiceMock.Setup(p => p.GetAsync(1)).Returns(Task.FromResult <BlogPost>(null)); var categoryServiceMock = new Mock <IBlogCategoryService>(); var controller = new BlogPostController(loggerMock.Object, postServiceMock.Object, categoryServiceMock.Object); var post = await controller.GetBlogPost(1); Assert.NotNull(post); var actionResult = Assert.IsType <ActionResult <BlogPostViewModel> >(post); var returnedMessage = Assert.IsType <NotFoundResult>(actionResult.Result); Assert.Equal((int)HttpStatusCode.NotFound, returnedMessage.StatusCode); }
public async Task Given_DatabaseHasBlogPostWithoutId_When_Get_Then_Return404() { var options = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(databaseName: "GetFail") .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.GetBlogPost(new Guid()); Assert.AreEqual(404, ((result as ActionResult <BlogPost>).Result as StatusCodeResult).StatusCode); } }
public async Task Given_DatabaseHasBlogPostWithId_When_Get_Then_ReturnBlogPost() { var options = new DbContextOptionsBuilder <Context>() .UseInMemoryDatabase(databaseName: "GetSuccess") .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.GetBlogPost(blogPosts[0].Id); Assert.AreEqual(blogPosts[0].Id, (result.Value as BlogPost).Id); } }
public async void WhenGetBlogPost_ReturnResult() { var loggerMock = new Mock <ILogger <BlogPostController> >(); var postMock = 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 postServiceMock = new Mock <IBlogPostService>(); postServiceMock.Setup(p => p.GetAsync(1)).Returns(Task.FromResult(postMock)); var categoryServiceMock = new Mock <IBlogCategoryService>(); var controller = new BlogPostController(loggerMock.Object, postServiceMock.Object, categoryServiceMock.Object); var post = await controller.GetBlogPost(1); Assert.NotNull(post); var actionResult = Assert.IsType <ActionResult <BlogPostViewModel> >(post); var returnedPost = Assert.IsType <BlogPostViewModel>(actionResult.Value); Assert.Equal(1, returnedPost.PostId); Assert.Equal("My first post", returnedPost.Title); Assert.Equal("Hello!", returnedPost.Body); }