Beispiel #1
0
        public void EditNews_WithCorrectData_ShouldReturn200OkModifiesTheNewsInCorrectWay()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Update(It.IsAny <News>())).Callback((News n) => news[CorrectNewsId - 1] = n);

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            var bindigModel = new NewsBindingModel()
            {
                Title         = "Vremeto",
                Content       = "...",
                PublishedDate = DateTime.Now.ToString("D")
            };

            var result = controller.EditMock(CorrectNewsId, bindigModel).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual(bindigModel.Title, news.First().Title);
            Assert.AreEqual("Moda", news.Last().Title);
            Assert.IsTrue(news.Count == 3);
        }
Beispiel #2
0
        public IHttpActionResult EditMock(int id, NewsBindingModel model)
        {
            var mockNews = MockDb.News().FirstOrDefault(n => n.Id == id);

            if (mockNews == null)
            {
                return(this.BadRequest("Non-existing news."));
            }

            if (model.Title == null && model.Content == null && model.PublishedDate == null)
            {
                return(this.BadRequest("Enter valid data."));
            }

            if (model.Title != null)
            {
                mockNews.Title = model.Title;
            }

            if (model.Content != null)
            {
                mockNews.Content = model.Content;
            }

            if (model.PublishedDate != null)
            {
                mockNews.PublishedDate = model.PublishedDate;
            }

            this.NewsRepo.Update(mockNews);

            return(this.Ok(mockNews));
        }
Beispiel #3
0
        public void PostNews_WithCorrectData_ShouldReturn201CreatedAndCreatedNews()
        {
            var news = MockDb.News();

            var mockRepository = new Mock <IRepository <News> >();

            mockRepository.Setup(r => r.Add(It.IsAny <News>())).Callback((News n) => news.Add(n));

            var controller = new NewsController(mockRepository.Object);

            SetupController(controller, "news");

            var bindigModel = new NewsBindingModel()
            {
                Title         = "Novina",
                Content       = "...",
                PublishedDate = DateTime.Now.ToString("D")
            };

            var result = controller.PostNews(bindigModel).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);
            Assert.AreEqual("Novini", news.First().Title);
            Assert.AreEqual(bindigModel.Title, news.Last().Title);
            Assert.IsTrue(news.Count == 4);
        }
Beispiel #4
0
        public IHttpActionResult DeleteMock(int id)
        {
            var mockNews = MockDb.News().FirstOrDefault(n => n.Id == id);

            if (mockNews == null)
            {
                return(this.BadRequest("Non-existing news."));
            }

            this.NewsRepo.Delete(mockNews);

            return(this.Ok("The news is delete."));
        }
Beispiel #5
0
        public void DeleteNews_ThatDoesNotExists_ShouldReturn400BadRequest()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Delete(It.IsAny <News>())).Callback((News n) => news.RemoveAt(3));

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            var result = controller.DeleteMock(IncorrectNewsId).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
            Assert.IsTrue(news.Count == 3);
        }
Beispiel #6
0
        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);
        }
Beispiel #7
0
        public void DeleteNews_ThatExists_ShouldReturn200OkAndDeleteTheNews()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Delete(It.IsAny <News>())).Callback((News n) => news.RemoveAt(CorrectNewsId - 1));

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            var result = controller.DeleteMock(CorrectNewsId).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.AreEqual("Sport", news.First().Title);
            Assert.IsTrue(news.Count == 2);
        }
Beispiel #8
0
        public void EditNews_WithIncorrectData_ShouldReturn400BadRequest()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Update(It.IsAny <News>())).Callback((News n) => news[CorrectNewsId - 1] = n);

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            //Incorect data - empty BindigModel
            var bindigModel = new NewsBindingModel();

            var result = controller.EditMock(CorrectNewsId, bindigModel).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
Beispiel #9
0
        public void PostNews_ThatIsIncorrect_ShouldThorw400BadRequeis()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Add(It.IsAny <News>())).Callback((News n) => news.Add(n));

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            var bindigModel = new NewsBindingModel()
            {
                Content       = "...",
                PublishedDate = DateTime.Now.ToString("D")
            };

            var result = controller.PostNews(bindigModel).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
Beispiel #10
0
        public void EditNews_ThatDoesNotExists_ShouldReturn400BadRequest()
        {
            var news = MockDb.News();

            var mockRepo = new Mock <IRepository <News> >();

            mockRepo.Setup(n => n.Update(It.IsAny <News>())).Callback((News n) => news[IncorrectNewsId - 1] = n);

            var controller = new NewsController(mockRepo.Object);

            SetupController(controller, "news");

            var bindigModel = new NewsBindingModel()
            {
                Title         = "Vremeto",
                Content       = "...",
                PublishedDate = DateTime.Now.ToString("D")
            };

            var result = controller.EditMock(IncorrectNewsId, bindigModel).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }