public void CreateNews_WithInvalidNews_ReturnsBadRequest() { // Arange var mockRepository = new Mock<INewsData>(); var controller = new NewsController(mockRepository.Object); // Act IHttpActionResult result = controller.Post(new News { }); // Assert Assert.IsInstanceOfType(result, typeof(BadRequestResult)); }
public void CreateNews_WithValidNews_ReturnsCreatedAndLocation() { // Arange var mockRepository = new Mock<INewsData>(); List<News> news = new List<News>(); mockRepository.Setup(rep => rep.News.Add(It.IsAny<News>())).Callback((News n) => news.Add(n)); var controller = new NewsController(mockRepository.Object); // Act IHttpActionResult result = controller.Post(new News { Id = 100, Title = "News number 1", Content = "Content of first news", PublishDate = DateTime.Now }); var createdResult = result as CreatedAtRouteNegotiatedContentResult<News>; // Assert Assert.IsNotNull(createdResult); Assert.AreEqual("DefaultApi", createdResult.RouteName); Assert.AreEqual(100, createdResult.RouteValues["id"]); Assert.IsInstanceOfType(result, typeof(CreatedAtRouteNegotiatedContentResult<News>)); Assert.IsNotNull(createdResult.Content); }