public void UpdateNews_WhenDataIsCorrect_ShouldReturnStatusCodeOk() { var repo = CreateRepo(); var controller = new NewsController(repo); SetupController(controller, "news"); var news = new NewsPostBindingModel() { Title = "Updated News", Content = "Updated news content", PublishDate = DateTime.Parse("22.01.2014") }; var result = controller.UpdateNews(1, news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); // Assert the repository values are correct Assert.AreEqual(3, repo.News.Count()); var newsInRepo = repo.News[0]; Assert.AreEqual("Updated News", newsInRepo.Title); Assert.AreEqual("Updated news content", newsInRepo.Content); Assert.AreEqual(new DateTime(2014, 1, 22), newsInRepo.PublishDate); Assert.AreEqual(1, newsInRepo.Id); Assert.IsTrue(repo.IsSaved); }
public void UpdateNews_WhenNewsIdIsInvalid_ShouldReturnStatusCodeBadRequest() { var repo = CreateRepo(); var controller = new NewsController(repo); SetupController(controller, "news"); var news = new NewsPostBindingModel() { Title = "Updated Title", Content = "Content4", PublishDate = DateTime.Parse("22.01.2014") }; var result = controller.UpdateNews(4, news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public IHttpActionResult PostNews([FromBody] NewsPostBindingModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var news = new News.Models.News() { Title = model.Title, Content = model.Content, PublishDate = model.PublishDate }; news = this.repo.Add(news); this.repo.SaveChanges(); return(this.CreatedAtRoute("DefaultApi", new { id = news.Id }, news)); }
public void PostNews_WhenDataIsInvalid_ShouldReturnStatusCodeBadRequest() { var repo = CreateRepo(); var controller = new NewsController(repo); SetupController(controller, "news"); controller.ModelState.AddModelError("fakeError", "fakeError"); var news = new NewsPostBindingModel() { Content = "Content4", PublishDate = DateTime.Parse("22.01.2014") }; var result = controller.PostNews(news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); news = new NewsPostBindingModel() { Title = "News4", PublishDate = DateTime.Parse("22.01.2014") }; result = controller.PostNews(news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); news = new NewsPostBindingModel() { Title = "News4", Content = "Content4" }; result = controller.PostNews(news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);; }
public IHttpActionResult UpdateNews([FromUri] int id, [FromBody] NewsPostBindingModel model) { if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } var news = repo.Find(id); if (news == null) { return(this.BadRequest("No news with id " + id)); } news.Title = model.Title; news.Content = model.Content; news.PublishDate = model.PublishDate; repo.Update(news); repo.SaveChanges(); return(this.Ok()); }
public void PostNews_WhenDataIsCorrect_ShouldReturnStatusCodeCreatedAndAddedNews() { var repo = CreateRepo(); var controller = new NewsController(repo); SetupController(controller, "news"); var news = new NewsPostBindingModel() { Title = "News4", Content = "Content4", PublishDate = DateTime.Parse("22.01.2014") }; var result = controller.PostNews(news).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); Assert.IsNotNull(result.Headers.Location); var resultContent = result.Content.ReadAsAsync <News.Models.News>().Result; Assert.AreEqual("News4", resultContent.Title); Assert.AreEqual("Content4", resultContent.Content); Assert.AreEqual(new DateTime(2014, 1, 22), resultContent.PublishDate); Assert.AreEqual(4, resultContent.Id); // Assert the repository values are correct Assert.AreEqual(4, repo.News.Count()); var newsInRepo = repo.News[3]; Assert.AreEqual("News4", newsInRepo.Title); Assert.AreEqual("Content4", newsInRepo.Content); Assert.AreEqual(new DateTime(2014, 1, 22), newsInRepo.PublishDate); Assert.AreEqual(4, newsInRepo.Id); Assert.IsTrue(repo.IsSaved); }