Пример #1
0
        public async Task ArticlePut_Item()
        {
            // Model
            var modelPut = new ArticleModel()
            {
                Id      = 1,
                Title   = "Title Altered",
                Content = "Content Altered",
            };

            // Arrange
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build()));

            // Act
            var result = await _articlesController.Put(1, modelPut);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var content = objectResult.Value as Article;

            Assert.NotNull(content);

            Assert.Equal(modelPut.Id, content.Id);
            Assert.Equal(modelPut.Content, content.Content);
            Assert.Equal(modelPut.Title, content.Title);
        }
Пример #2
0
        public async void Put_ShouldReturn_UpdatedArticle()
        {
            // Arrange
            mockedRep.Setup(x => x.UpdateArticleAsync(It.IsAny <int>(), It.IsAny <Article>())).ReturnsAsync(new Article());

            // Act
            var actual = await sut.Put(0, new Article());

            // Assert
            Assert.IsType(typeof(JsonResult <Article>), actual);
        }
        public async Task Put_InvalidArticle_ReturnsBadRequest()
        {
            //Arrange
            _articlesController.ModelState.AddModelError("ArticleBody", "Required");

            //Act
            var result = await _articlesController.Put(ObjectId.GenerateNewId().ToString(), TestHelper.DummyArticles(1).Single());

            //Assert
            Assert.IsInstanceOf <BadRequestObjectResult>(result);
        }
Пример #4
0
        public async Task Put_UpdateITem()
        {
            // Arrange
            _context.Articles.Add(Builder <Article> .CreateNew().Build());
            _context.SaveChanges();

            var articleModel = new ArticleModel {
                Title = "TitleUpdated"
            };

            // Act
            var result = await _articlesController.Put(_context.Articles.FirstOrDefault().Id, articleModel);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var content = objectResult.Value as Article;

            Assert.NotNull(content);

            Assert.Equal("TitleUpdated", content.Title);
        }
        public async Task Put_BadRequest()
        {
            //Arrange
            _articlesController.ModelState.Clear();
            _articlesController.ModelState.AddModelError("Title", "Title is Required");

            //Act
            var result = await _articlesController.Put(It.IsAny <int> (), It.IsAny <ArticleModel> ());

            //Assert
            Assert.NotNull(result);

            var objectResult = result as BadRequestObjectResult;

            Assert.NotNull(objectResult);
        }
Пример #6
0
        public async Task Put_Article_Edit()
        {
            // Arrange
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build()));
            var articleDbSetMock = Builder <Article> .CreateNew().Build();

            _articleRepositoryMock.Setup(m => m.UpdateAsync(articleDbSetMock));
            var articleModel = new ArticleModel
            {
                Title     = "TitleEdited",
                Content   = "ContentEdited",
                Published = articleDbSetMock.Published
            };

            // Act
            var result = await _articlesController.Put(1, articleModel);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var content = objectResult.Value as ArticleModel;

            Assert.NotNull(content);

            Assert.Equal(articleModel.Title, content.Title);
            Assert.Equal(articleModel.Content, content.Content);
            Assert.Equal(DateTime.UtcNow.ToLocalTime().Date, content.Date.Date);
            Assert.Equal(articleModel.Published, content.Published);
        }
Пример #7
0
        public async Task Put_Article()
        {
            var article = new Article()
            {
                Id = 1, Title = "Put article Test"
            };
            var articleModel = new ArticleModel()
            {
                Id = 1, Title = "Put article Test"
            };

            // Arrange
            _articleRepositoryMock.Setup(m => m.UpdateAsync(article)).Returns(Task.FromResult <Article>(article));
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(article));

            // Act
            var result = await _articlesController.Put(1, articleModel);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var content = objectResult.Value as Article;

            Assert.NotNull(content);

            Assert.Equal(200, objectResult.StatusCode);

            Assert.Equal("Put article Test", content.Title);
            Assert.Equal(1, content.Id);
        }
Пример #8
0
        public async Task Put_ReturnsBadRequest()
        {
            // Arrange
            _articlesController.ModelState.AddModelError("Content", "Content is required");
            var article = Builder <ArticleModel> .CreateNew().Build();

            //Act
            var result = await _articlesController.Put(0, article);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as BadRequestObjectResult;

            Assert.NotNull(objectResult);
            Assert.Equal(400, objectResult.StatusCode);
        }
Пример #9
0
        public async Task Update_NotFound()
        {
            // Arrange
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(null));

            // Act
            var model = Builder <ArticleModel> .CreateNew().Build();

            var result = await _articlesController.Put(1, model);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as NotFoundResult;

            Assert.NotNull(objectResult);
        }
Пример #10
0
        public async Task Put_Success()
        {
            // Arrange
            _articleRepositoryMock.Setup(m => m.UpdateAsync(Builder <Article> .CreateNew().Build()));
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build()));

            // Act
            var result = await _articlesController.Put(1, new ArticleModel());

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var content = objectResult.Value as Article;

            Assert.NotNull(content);
        }
Пример #11
0
        public async Task Put_Ok()
        {
            // Arrange
            _articleRepositoryMock.Setup(m => m.GetAsync(0)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build()));
            //_articleRepositoryMock.Setup(m => m.UpdateAsync(0)).Returns(Task.FromResult<Article>(Builder<Article>.CreateNew().Build()));

            // Act
            var result = await _articlesController.Put(Builder <int> .CreateNew().Build(), Builder <ArticleModel> .CreateNew().Build());

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);
        }
Пример #12
0
        public async Task Put_Article_InvalidState()
        {
            // Arrange
            var mockArticleModel = new ArticleModel {
                Title = "mock article"
            };

            _articlesController.ModelState.AddModelError("Description", "This field is required");
            // Act
            var result = await _articlesController.Put(0, mockArticleModel);

            // Assert
            var actionResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal(new SerializableError(_articlesController.ModelState), actionResult.Value);
        }
Пример #13
0
        public async Task Put_BadRequest()
        {
            // Arrange
            var controller = new ArticlesController(_articleRepositoryMock.Object);

            controller.ModelState.AddModelError("Content", "Required");

            // Act
            var result = await controller.Put(Builder <int> .CreateNew().Build(), Builder <ArticleModel> .CreateNew().Build());

            // Assert
            Assert.NotNull(result);

            var objectResult = result as BadRequestObjectResult;

            Assert.NotNull(objectResult);
        }
        public async Task UpdateItemData()
        {
            // Arrange
            _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build()));

            // Act
            var result = await _articlesController.Get(1);

            // Assert
            Assert.NotNull(result);

            var objectResult = result as OkObjectResult;

            Assert.NotNull(objectResult);

            var beforeUpdate = objectResult.Value as ArticleModel;

            Assert.NotNull(beforeUpdate);

            var artitleModel = new ArticleModel
            {
                Title   = "new title",
                Content = "new content"
            };

            result = await _articlesController.Put(1, artitleModel);

            Assert.NotNull(result);
            //update success
            objectResult = result as OkObjectResult;
            Assert.NotNull(objectResult);
            // Act
            result = await _articlesController.Get(1);

            // Assert
            Assert.NotNull(result);

            objectResult = result as OkObjectResult;
            Assert.NotNull(objectResult);

            var afterUpdate = objectResult.Value as ArticleModel;

            Assert.NotNull(afterUpdate);
            Assert.Equal(artitleModel.Title, afterUpdate.Title);
            Assert.Equal(artitleModel.Content, afterUpdate.Content);
        }