public ActionResult <bool> PostArticle(Article article)
 {
     if (string.IsNullOrEmpty(article.Article_Id))
     {
         return(_articlesService.CreateArticle(article));
     }
     else
     {
         return(_articlesService.UpdateArticle(article));
     }
 }
Example #2
0
        public void CreateArticle_ReceivesCreateArticleRequest_ExpectsArticleModel()
        {
            var username  = "******";
            var title     = "My Article 1";
            var content   = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
            var thumbnail = new byte[] { 72, 101, 108, 108, 111 };

            var newArticle = new Article(
                username,
                title,
                content,
                thumbnail);

            var mockArticlesRepo = new Mock <IArticlesRepository>();

            mockArticlesRepo
            .Setup(x => x.CreateAsync(It.IsAny <Article>()))
            .ReturnsAsync(newArticle);

            var mockCommentsRepo = new Mock <ICommentsRepository>();

            var mockLogger = new Mock <ILogger <ArticlesService> >();

            var mockValidationFactory = new Mock <IValidatorFactory>();

            mockValidationFactory.Setup(x =>
                                        x.GetValidator <CreateArticleRequest>()).Returns(new CreateArticleRequestValidator());

            var mockContext = GetServerCallContext("CreateArticle");

            var service = new ArticlesService(
                mockLogger.Object,
                mockArticlesRepo.Object,
                mockCommentsRepo.Object,
                mockValidationFactory.Object);

            var request = new CreateArticleRequest
            {
                User           = username,
                Title          = title,
                Content        = content,
                ThumbnailImage = ByteString.CopyFrom(thumbnail),
            };

            var result = service.CreateArticle(request, mockContext).Result;

            Assert.Equal(result.User, request.User);
            Assert.Equal(result.Title, request.Title);
            Assert.Equal(result.Content, request.Content);
            Assert.Equal(result.ThumbnailImage, request.ThumbnailImage);
        }