public void AddNews_InValidNewsAddedToDb_ShouldThrowException()
        {
            // Arrange -> prapare the objects
            var newInvalidNews = new News { Title = "Invalid", Content = "Something", PublishDate = DateTime.Now };
            IRepository<News> repository = new Repository<News>();

            // Act -> perform some logic
            repository.Add(newInvalidNews);
            repository.SaveChanges();
        }
        public void AddNews_ValidNewsAddedToDb_ShouldReturnNews()
        {
            // Arrange -> prapare the objects
            var newValidNews = new News { Title = "Valid news added", Content = "Something", PublishDate = DateTime.Now };
            IRepository<News> repository = new Repository<News>();

            // Act -> perform some logic
            repository.Add(newValidNews);
            repository.SaveChanges();

            // Assert -> validate the results
            var newsFromDb = repository.Find(newValidNews.Id);

            Assert.IsNotNull(newsFromDb);
            Assert.AreEqual(newValidNews.Title, newsFromDb.Title);
            Assert.AreEqual(newValidNews.Content, newsFromDb.Content);
            Assert.AreEqual(newValidNews.PublishDate, newsFromDb.PublishDate);
            Assert.IsTrue(newsFromDb.Id != 0);
        }
Example #3
0
        public void UpdateNews_WithValidNews_ReturnsOKAndModifiedNews()
        {
            // Arange
            var mockRepository = new Mock<INewsData>();
            News[] news = 
            {
                new News { Id = 100, Title = "News number 1", Content = "Content of first news", PublishDate = DateTime.Now }
            };

            mockRepository.Setup(rep => rep.News.Update(It.IsAny<News>())).Verifiable();
            mockRepository.Setup(rep => rep.News.Find(It.IsAny<int>())).Returns(news.First());
            var modifiedNewsItem = new News { Id = 100, Title = "Modified title", Content = "Content of first news", PublishDate = DateTime.Now };

            var controller = new NewsController(mockRepository.Object);

            // Act
            IHttpActionResult result = controller.Put(modifiedNewsItem);
            var updatedResult = result as NegotiatedContentResult<News>;

            // Assert
            Assert.IsNotNull(updatedResult);
            Assert.AreEqual("Modified title", updatedResult.Content.Title);
            Assert.IsInstanceOfType(result, typeof(NegotiatedContentResult<News>));
            Assert.IsNotNull(updatedResult.Content);
        }
Example #4
0
        public void DeleteNews_ExisitngNews_ReturnsOKAndModifiedNews()
        {
            // Arange
            var mockRepository = new Mock<INewsData>();
            var newsItem = new News
            {
                Id = 100,
                Title = "News number 1",
                Content = "Content of first news",
                PublishDate = DateTime.Now
            };

            var news = new List<News> 
            {
                newsItem
            };

            mockRepository.Setup(rep => rep.News.Delete(It.IsAny<int>())).Callback(()=>news.Remove(newsItem));
            mockRepository.Setup(rep => rep.News.Find(It.IsAny<int>())).Returns(news.First());

            var controller = new NewsController(mockRepository.Object);

            // Act
            IHttpActionResult result = controller.Delete(100);

            // Assert
            Assert.IsInstanceOfType(result, typeof(OkResult));
            Assert.AreEqual(0, news.Count);
        }
Example #5
0
        public void UpdateNews_WithNonExisitngNewsNews_ReturnsBadRequest()
        {
            // Arange
            var mockRepository = new Mock<INewsData>();
            News[] news = 
            {
                new News { Id = 100, Title = "News number 1", Content = "Content of first news", PublishDate = DateTime.Now }
            };

            mockRepository.Setup(rep => rep.News.Update(It.IsAny<News>())).Verifiable();
            mockRepository.Setup(rep => rep.News.Find(It.IsAny<int>())).Returns(news.First());
            var modifiedNewsItem = new News { Id = 1000, Title = "Modified title", Content = "Content of first news", PublishDate = DateTime.Now };

            var controller = new NewsController(mockRepository.Object);

            // Act
            IHttpActionResult result = controller.Put(modifiedNewsItem);

            // Assert
            Assert.IsInstanceOfType(result, typeof(BadRequestResult));
        }