Exemplo n.º 1
0
        public async Task AddNewsShouldThrowException()
        {
            string userId = "Jack";
            News   news   = new News {
                Title = "chandrayaan2-spacecraft", Content = "The Lander of Chandrayaan-2 was named Vikram after Dr Vikram A Sarabhai", PublishedAt = DateTime.Now
            };
            var mockRepo = new Mock <INewsRepository>();

            mockRepo.Setup(repo => repo.IsNewsExist(userId, news.Title)).Returns(Task.FromResult(true));
            var service = new NewsService.Services.NewsService(mockRepo.Object);

            var actual = await Assert.ThrowsAsync <NewsAlreadyExistsException>(() => service.CreateNews(userId, news));

            Assert.Equal($"{userId} have already added this news", actual.Message);
        }
Exemplo n.º 2
0
        public async Task AddNewsShouldReturnNews()
        {
            string userId = "Jack";
            News   news   = new News {
                Title = "chandrayaan2-spacecraft", Content = "The Lander of Chandrayaan-2 was named Vikram after Dr Vikram A Sarabhai", PublishedAt = DateTime.Now
            };
            int newsId   = 103;
            var mockRepo = new Mock <INewsRepository>();

            mockRepo.Setup(repo => repo.IsNewsExist(userId, news.Title)).Returns(Task.FromResult(false));
            mockRepo.Setup(repo => repo.CreateNews(userId, news)).Returns(Task.FromResult(newsId));
            var service = new NewsService.Services.NewsService(mockRepo.Object);

            var actual = await service.CreateNews(userId, news);

            Assert.IsAssignableFrom <int>(actual);
            Assert.Equal(103, actual);
        }