コード例 #1
0
        public async Task Create_ReturnsACreatedAtActionResultAndAddsEpisode_WhenModelStateIsValid()
        {
            var mapper = CreateAutoMapper();

            var episodeServiceMock = new Mock <IEpisodeService>();

            episodeServiceMock.Setup(s => s.CreateAsync(It.IsAny <Episode>()))
            .Returns(Task.FromResult(new Episode()))
            .Verifiable();

            var controller = new EpisodeController(mapper, episodeServiceMock.Object);
            var episode    = new CreateEpisodeViewModel
            {
                Title   = "Episode 1",
                VideoId = "aljklasf"
            };

            var result = await controller.Create(episode);

            var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result);

            Assert.Equal("GetById", createdAtActionResult.ActionName);
            Assert.IsType <EpisodeViewModel>(createdAtActionResult.Value);
            episodeServiceMock.Verify();
        }
コード例 #2
0
        public async Task Create_ReturnsBadRequestResult_WhenModelStateIsInvalid()
        {
            var mapper = CreateAutoMapper();

            var episodeServiceMock = new Mock <IEpisodeService>();

            episodeServiceMock.Setup(s => s.CreateAsync(It.IsAny <Episode>()))
            .Returns(Task.FromResult(new Episode()));

            var controller = new EpisodeController(mapper, episodeServiceMock.Object);

            controller.ModelState.AddModelError("Title", "A title is required");

            var newEpisode = new CreateEpisodeViewModel();

            var result = await controller.Create(newEpisode);

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

            Assert.IsType <SerializableError>(badRequestResult.Value);
        }