public void Listing_All_News_Should_Return_Them_Correctly() { var fakeNews = _mocks.NewsRepoMock.Object.All(); var mockContext = new Mock <INewsData>(); mockContext.Setup(c => c.News.All()) .Returns(fakeNews); var newsController = new NewsController(mockContext.Object); SetupController(newsController); var response = newsController.GetAllNews().ExecuteAsync(CancellationToken.None).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var newsResponse = response.Content.ReadAsAsync <IEnumerable <NewsViewModel> >() .Result .Select(n => n.Id) .ToList(); var fakeNewsList = fakeNews .OrderByDescending(n => n.PublishDate) .Select(n => n.Id) .ToList(); CollectionAssert.AreEqual(newsResponse, fakeNewsList); }
public void UpdateNews_WithIncorrectData_ShouldReturnBadRequestWithOutUpdatingTheNews() { // Arrange var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", }; var controller = new NewsController(this.dataLayerMocked); this.SetupController(controller, "news"); // Act this.dataLayerMocked.News.Add(initialNew); var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; var newsFromService = httpGetResponse.Content.ReadAsAsync <List <News> >().Result; var newsId = newsFromService[0].Id; var httpPutResponse = controller.UpdateNewById(newsId, null).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.BadRequest, httpPutResponse.StatusCode); Assert.AreEqual(initialNew.Title, newsFromService[0].Title); Assert.AreEqual(initialNew.Content, newsFromService[0].Content); }
public void ListAllNewsItems_WhenCorrect_ShouldReturnAllNewsItems() { var repo = CreateRepo(); var controller = new NewsController(repo); var news = controller.GetAllNews(); SetupController(controller, "news"); Assert.AreEqual(3, news.Count()); var testedNews = news.Where(n => n.Title == "News1").FirstOrDefault(); Assert.AreEqual("News1", testedNews.Title); Assert.AreEqual("Content1", testedNews.Content); Assert.AreEqual(new DateTime(2015, 1, 20).ToShortDateString(), testedNews.PublishDate.ToShortDateString()); testedNews = news.Where(n => n.Title == "News2").FirstOrDefault(); Assert.AreEqual("News2", testedNews.Title); Assert.AreEqual("Content2", testedNews.Content); Assert.AreEqual(new DateTime(2015, 1, 21).ToShortDateString(), testedNews.PublishDate.ToShortDateString()); testedNews = news.Where(n => n.Title == "News3").FirstOrDefault(); Assert.AreEqual("News3", testedNews.Title); Assert.AreEqual("Content3", testedNews.Content); Assert.AreEqual(new DateTime(2015, 1, 20).ToShortDateString(), testedNews.PublishDate.ToShortDateString()); }
public void UpdateNews_WithCorrectData_ShouldUpdateTheNewsAndReturn200Ok() { // Arrange var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", }; var controller = new NewsController(this.dataLayerMocked); this.SetupController(controller, "news"); // Act this.dataLayerMocked.News.Add(initialNew); var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; var newsFromService = httpGetResponse.Content.ReadAsAsync <List <News> >().Result; var updatedNew = new UpdateNewInputModel() { Title = "Updated", Content = "Tralala" }; var newsId = newsFromService[0].Id; var httpPutResponse = controller.UpdateNewById(newsId, updatedNew).ExecuteAsync(new CancellationToken()).Result; var result = httpPutResponse.Content.ReadAsAsync <News>().Result; // Assert Assert.AreEqual(updatedNew.Title, result.Title); Assert.AreEqual(updatedNew.Content, result.Content); }
public void NewsControllerGetAllNewsShould_ReturnOkStatusCode() { var db = this.Context; this.PopulateData(db); var newsController = new NewsController(db); Assert.IsType <OkObjectResult>(newsController.GetAllNews()); }
public void NewsControllerGetAllNewsShould_ReturnCorrectData() { //Arrenge var db = this.Context; this.PopulateData(db); var newsController = new NewsController(db); //Act var news = newsController.GetAllNews(); //Assert var returnedData = (newsController.GetAllNews() as OkObjectResult).Value as IEnumerable <NewsEnt>; var testData = this.GetTestData(); foreach (var returnedModel in returnedData) { var testModel = testData.First(n => n.Id == returnedModel.Id); Assert.NotNull(testModel); Assert.True(this.CompareNewsExact(returnedModel, testModel)); } }
public void CreateNews_WithIncorrectData_ShouldReturnBadRequestWithOutAddingTheNews() { // Arrange var controller = new NewsController(this.dataLayerMocked); this.SetupController(controller, "news"); // Act var httpResponse = controller.CreateNew(null).ExecuteAsync(new CancellationToken()).Result; var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; var newsFromService = httpGetResponse.Content.ReadAsAsync <News[]>().Result; // Arrange Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode); Assert.AreEqual(0, newsFromService.Count()); }
public void GetAllNews_WithCorrectData_ShouldReturn201CreatedAndCreatedNews() { var news = MockDb.News(); var mockRepository = new Mock <IRepository <News> >(); mockRepository.Setup(r => r.All()).Returns(news.AsQueryable()); var controller = new NewsController(mockRepository.Object); SetupController(controller, "news"); var result = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); Assert.AreEqual("Novini", news.First().Title); Assert.IsTrue(news.Count == 3); }
public void NewsControllerGetAllNewsShould_ReturnOkStatusCode() { var context = this.Context; this.PopulateData(context); var newsController = new NewsController(context); var returnedData = (newsController.GetAllNews() as OkObjectResult).Value as IEnumerable <Data.Models.News>; var testData = this.GetTestData(); foreach (var returnedModel in returnedData) { var testModel = testData.First(n => n.Id == returnedModel.Id); Assert.NotNull(testModel); Assert.True(this.CompareNewsExact(returnedModel, testModel)); } }
public void GetAll_WhenValid_ShouldReturnBugsCollection() { // Arrange // var repo = new NewsRepositoryMock(); var newsData = this.dataLayerMocked.News; var news = AddThreeNews(); newsData.Add(new News() { Title = "Test news #1", Content = "Test content #1", PublishedAt = new DateTime(2014, 1, 1) }); newsData.Add(new News() { Title = "Test news #2", Content = "Test content #2", PublishedAt = new DateTime(2014, 1, 1) }); newsData.Add(new News() { Title = "Test news #3", Content = "Test content #3", PublishedAt = new DateTime(2014, 1, 1) }); var controller = new NewsController(dataLayerMocked); this.SetupController(controller, "news"); // Act var httpResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; var result = httpResponse.Content.ReadAsAsync <List <News> >().Result; // Assert Assert.AreEqual(news.Count, result.Count); Assert.AreEqual(news[0].Title, result[0].Title); Assert.AreEqual(news[1].Title, result[1].Title); Assert.AreEqual(news[2].Content, result[2].Content); }
public void Delete_WithNonExistingNews_ShouldDeleteTheNewsAndReturn404NotFound() { // Arrange var initialNew = new News() { Id = 1, Title = "Test news #1", Content = "Test content #1", }; var controller = new NewsController(this.dataLayerMocked); this.SetupController(controller, "news"); // Act this.dataLayerMocked.News.Add(initialNew); var httpGetResponse = controller.GetAllNews().ExecuteAsync(new CancellationToken()).Result; var newsFromService = httpGetResponse.Content.ReadAsAsync <List <News> >().Result; var newsId = 3; // Non existing news var httpDeleteResponse = controller.DeleteNewById(newsId).ExecuteAsync(new CancellationToken()).Result; Assert.AreEqual(1, this.dataLayerMocked.News.All().Count()); Assert.AreEqual(HttpStatusCode.NotFound, httpDeleteResponse.StatusCode); }
public void TestGetAllPostWhenHasNewsShouldReturnNewsSortedDescendingByDate() { // Setup fake news var fakeNews = new List <News>() { new News() { Id = 1, Title = "1", Content = "1", PublishDate = new DateTime(2000, 01, 01) }, new News() { Id = 2, Title = "2", Content = "2", PublishDate = new DateTime(2000, 01, 02) }, new News() { Id = 3, Title = "3", Content = "3", PublishDate = new DateTime(2000, 01, 03) }, new News() { Id = 4, Title = "4", Content = "4", PublishDate = new DateTime(2000, 01, 04) }, }; // Setup repositories var mockedRepository = new Mock <IRepository <News> >(); mockedRepository.Setup(r => r.All()).Returns(fakeNews.AsQueryable()); // Setup data layer var mockedContext = new Mock <INewsData>(); mockedContext.Setup(c => c.News).Returns(mockedRepository.Object); // Setup controller var controller = new NewsController(mockedContext.Object); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); var response = controller.GetAllNews().ExecuteAsync(CancellationToken.None).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); var news = response.Content .ReadAsAsync <IEnumerable <NewsViewModel> >() .Result .Select(n => n.Title) .ToList(); var orderedFakenews = fakeNews .OrderByDescending(n => n.PublishDate) .Select(n => n.Title) .ToList(); CollectionAssert.AreEqual(orderedFakenews, news); }