コード例 #1
0
ファイル: NewsControllerTests.cs プロジェクト: nayots/SoftUni
        public void ModifyItemWithBadDataReturnsBadRequestResponse()
        {
            //Arrange
            TestUtils testUtils = new TestUtils();

            TestUtils.InitializeMapper();
            var db = testUtils.GetDbContext();

            this.PopulateDb(db);

            INewsService newsService = new NewsService(db);

            var sut   = new NewsController(newsService);
            var model = new CreateEditNewsModel()
            {
                Title       = "",
                Content     = "",
                PublishDate = new DateTime(2017, 12, 12)
            };
            //Act

            var result = sut.Update(999, model);

            //Assert

            Assert.IsType <BadRequestObjectResult>(result);
        }
コード例 #2
0
ファイル: NewsControllerTests.cs プロジェクト: nayots/SoftUni
        public void ModifyItemWithCorrectDataReturnsOkResponseWithItem()
        {
            //Arrange
            TestUtils testUtils = new TestUtils();

            TestUtils.InitializeMapper();
            var db = testUtils.GetDbContext();

            this.PopulateDb(db);

            INewsService newsService = new NewsService(db);

            var sut   = new NewsController(newsService);
            var model = new CreateEditNewsModel()
            {
                Title       = "TestTitleEdit",
                Content     = "BlablaEdit",
                PublishDate = new DateTime(2017, 12, 12)
            };
            //Act

            var result = sut.Update(1, model);

            //Assert

            Assert.IsType <OkObjectResult>(result);

            var resultData = (result as OkObjectResult).Value as NewsInfoModel;

            Assert.True(resultData.Title == model.Title && resultData.Content == model.Content);
        }
コード例 #3
0
        public IActionResult Create([FromBody] CreateEditNewsModel createEditNewsModel)
        {
            if (this.ModelState.IsValid)
            {
                var result = this.newsService.Create(createEditNewsModel.Title, createEditNewsModel.Content, createEditNewsModel.PublishDate.Value);

                if (result != null)
                {
                    return(this.Ok(result));
                }
            }

            return(BadRequest(this.ModelState));
        }
コード例 #4
0
        public IActionResult Update(int?id, [FromBody] CreateEditNewsModel createEditNewsModel)
        {
            if (id.HasValue && this.ModelState.IsValid && this.newsService.Exists(id.Value))
            {
                var result = this.newsService.Update(id.Value, createEditNewsModel.Title, createEditNewsModel.Content, createEditNewsModel.PublishDate.Value);

                if (result != null)
                {
                    return(this.Ok(result));
                }
            }
            if (id is null || !this.newsService.Exists(id.Value))
            {
                this.ModelState.AddModelError("Id", GlobalConstants.NoValidIdProvided);
            }

            return(BadRequest(this.ModelState));
        }
コード例 #5
0
ファイル: NewsControllerTests.cs プロジェクト: nayots/SoftUni
        public void CreateItemReturnsOk()
        {
            //Arrange
            TestUtils testUtils = new TestUtils();

            var mockService = new Mock <INewsService>();

            mockService.Setup(
                m => m.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <DateTime>()))
            .Returns(() =>
                     new NewsInfoModel()
            {
                Id          = 1,
                Title       = "TestTitle",
                Content     = "Blabla",
                PublishDate = new DateTime(2017, 12, 12)
            });

            var sut   = new NewsController(mockService.Object);
            var model = new CreateEditNewsModel()
            {
                Title       = "TestTitle",
                Content     = "Blabla",
                PublishDate = new DateTime(2017, 12, 12)
            };

            //Act
            var result = sut.Create(model);

            //Assert

            Assert.IsType <OkObjectResult>(result);

            var resultData = (result as OkObjectResult).Value as NewsInfoModel;

            Assert.True(resultData.Title == model.Title
                        &&
                        resultData.Content == model.Content
                        &&
                        resultData.PublishDate == model.PublishDate);
        }