コード例 #1
0
        public void EditViewLoads()
        {
            //Act
            ViewResult result = songController.Edit(900) as ViewResult;

            //Assert
            Assert.AreEqual("Edit", result.ViewName);
        }
コード例 #2
0
        public async Task ReturnViewWithSuccessMessageWhenSongIsEditedSuccessfully()
        {
            string expectedMessage = "edited successfully";

            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongMock = new Mock <ICommandService <EditSong> >();

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongMock.Object);

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("success", result.Type);
            StringAssert.Contains(expectedMessage, result.Message);
        }
コード例 #3
0
        public async Task ReturnViewWithErrorMessageWhenCommandServiceThrowNotFoundException()
        {
            string expectedMessage = "Fake message";

            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongStub = new Mock <ICommandService <EditSong> >();

            editSongStub.Setup(u => u.ExecuteAsync(It.IsAny <EditSong>()))
            .Throws(new NotFoundException(expectedMessage));

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongStub.Object);

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            Assert.AreEqual(expectedMessage, result.Message);
        }
コード例 #4
0
        public void HaveRequiredAttribute(Type attrType)
        {
            // Arrange
            SongsController sut = CreateSongsController(Mock.Of <ICommandService <EditSong> >());

            // Act && Assert
            Assert.IsTrue(MethodHasAttribute(
                              () => sut.Edit(It.IsAny <string>(), default(SongFormModel)),
                              attrType));
        }
コード例 #5
0
        public void Edit()
        {
            // Arrange
            SongsController controller = new SongsController();

            // Act
            ViewResult result = controller.Edit(5) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
コード例 #6
0
        public void EditArtist()
        {
            // Arrange
            SongsController controller = new SongsController();

            // Act
            ViewResult result = controller.Edit(5) as ViewResult;

            // Assert
            Assert.AreEqual("ACDC", result.ViewBag.Artist);
            Assert.AreNotEqual("Greenday", result.ViewBag.Artist);
            Assert.IsNull(result.ViewBag.Artist);
            Assert.IsNotNull(result.ViewBag);
        }
コード例 #7
0
        public async Task SongController_GetEdit_ShouldReturnBadRequestWhenSongIsNull()
        {
            //Arrenge
            var adminSongService = this.GetAdminSongServiceBaseMock();

            adminSongService
            .Setup(s => s.GetByIdAsync(It.IsAny <int>()))
            .ReturnsAsync((AdminSongEditServiceModel)null);

            var controller = new SongsController(null, adminSongService.Object);
            //Act
            var result = await controller.Edit(1);

            //Assert
            result.Should().BeOfType <BadRequestResult>();
        }
コード例 #8
0
        public async Task SongController_GetEdit_ShouldReturnViewWithValidModel()
        {
            const string  SongName     = "TestSong";
            const double  SongDuration = 2;
            const decimal SongPrice    = 5;
            const string  SongArtist   = "ArtistName";
            //Arrenge
            var adminArtistService = this.GetAdminArtistServiceMock();
            var adminSongService   = this.GetAdminSongServiceBaseMock();

            adminSongService
            .Setup(s => s.GetByIdAsync(It.IsAny <int>()))
            .ReturnsAsync(new AdminSongEditServiceModel
            {
                Name     = SongName,
                Duration = SongDuration,
                Price    = SongPrice,
                Artist   = SongArtist
            });

            var controller = new SongsController(adminArtistService.Object, adminSongService.Object);

            //Act
            var result = await controller.Edit(1);


            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <SongFormViewModel>();

            var formModel = model.As <SongFormViewModel>();

            formModel.Name.Should().Be(SongName);
            formModel.Duration.Should().Be(SongDuration);
            formModel.Price.Should().Be(SongPrice);

            this.AssertArtistsSelectListItems(formModel.Artists);
        }
コード例 #9
0
        public async Task SongController_PostEdit_ShouldReuturnViewWithCorrectMethodWhenModelStateIsInvalid()
        {
            //Arrenge
            const string  SongName     = "TestSong";
            const double  SongDuration = 2;
            const decimal SongPrice    = 5;

            var adminSongService = this.GetAdminSongServiceBaseMock();

            adminSongService.Setup(s => s.ExistAsync(It.IsAny <int>()))
            .ReturnsAsync(false);
            var adminArtistService = this.GetAdminArtistServiceMock();

            var controller = new SongsController(adminArtistService.Object, adminSongService.Object);
            //Act
            var result = await controller.Edit(2, new SongFormViewModel
            {
                Name     = SongName,
                Duration = SongDuration,
                Price    = SongPrice,
                Ganre    = Ganre.Blues,
                ArtistId = 1
            });

            //Assert
            result.Should().BeOfType <ViewResult>();
            var model = result.As <ViewResult>().Model;

            model.Should().BeOfType <SongFormViewModel>();

            var formModel = model.As <SongFormViewModel>();

            formModel.Name.Should().Be(SongName);
            formModel.Duration.Should().Be(SongDuration);
            formModel.Price.Should().Be(SongPrice);

            this.AssertArtistsSelectListItems(formModel.Artists);
        }
コード例 #10
0
        public async Task ReturnViewWithErrorMessageWhenPassedFileTypeIsNotSong()
        {
            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "InvalidSongFile");

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: Mock.Of <ICommandService <EditSong> >());

            // Act
            var result = await sut.Edit(It.IsAny <string>(), model) as WithMessageResult;

            // Assert
            Assert.AreEqual(typeof(WithMessageResult), result.GetType());
            Assert.AreEqual("danger", result.Type);
            StringAssert.Contains("should be an audio", result.Message);
        }
コード例 #11
0
        public async Task TheEditSongCommandServiceOnce()
        {
            var fileStub = new Mock <IFormFile>();

            fileStub.Setup(f => f.ContentType).Returns(() => "audio");
            fileStub.Setup(f => f.Length).Returns(() => SongMaxLength - 1);

            var editSongMock = new Mock <ICommandService <EditSong> >();

            SongFormModel model = new SongFormModel()
            {
                File = fileStub.Object
            };

            // Arrange
            SongsController sut = CreateSongsController(
                editSong: editSongMock.Object);

            // Act
            await sut.Edit(It.IsAny <string>(), model);

            // Assert
            editSongMock.Verify(x => x.ExecuteAsync(It.IsAny <EditSong>()), Times.Once());
        }
コード例 #12
0
        public async Task SongController_PostEdit_ShoudReturnRedirectWithViewModel()
        {
            //Arrenge
            int     modelId        = 0;
            string  modelName      = null;
            decimal modelPrice     = 0;
            double  modelDuration  = 0;
            int     modelArtistId  = 0;
            Ganre   modelGanre     = 0;
            string  successMessage = null;

            int     resultModelId       = 1;
            string  resultModelName     = "TestSong";
            decimal resultModelPrice    = 2;
            double  resultModelDuration = 3;
            int     resultModelArtistId = 4;
            Ganre   resultModelGanre    = Ganre.Disco;

            var adminSongService = this.GetAdminSongServiceBaseMock();

            adminSongService.Setup(s => s.ExistAsync(It.IsAny <int>()))
            .ReturnsAsync(true);

            var adminArtistService = this.GetAdminArtistServiceMock();

            adminArtistService.Setup(a => a.ExistAsync(It.IsAny <int>()))
            .ReturnsAsync(true);

            adminSongService.Setup(s => s.EditAsync(
                                       It.IsAny <int>(),
                                       It.IsAny <string>(),
                                       It.IsAny <decimal>(),
                                       It.IsAny <double>(),
                                       It.IsAny <int>(),
                                       It.IsAny <Ganre>()
                                       ))
            .Callback((int id, string name, decimal price, double duration, int artistId, Ganre ganre) =>
            {
                modelId       = id;
                modelName     = name;
                modelPrice    = price;
                modelDuration = duration;
                modelArtistId = artistId;
                modelGanre    = ganre;
            })
            .Returns(Task.CompletedTask);

            adminSongService
            .Setup(a => a.IsGanreExist(It.IsAny <int>()))
            .Returns(true);

            var tempDate = new Mock <ITempDataDictionary>();

            tempDate.SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>())
            .Callback((string key, object message) => successMessage         = message as string);

            var controller = new SongsController(adminArtistService.Object, adminSongService.Object);

            controller.TempData = tempDate.Object;

            //Act
            var result = await controller.Edit(resultModelId, new SongFormViewModel
            {
                Name     = resultModelName,
                Price    = resultModelPrice,
                Duration = resultModelDuration,
                ArtistId = resultModelArtistId,
                Ganre    = resultModelGanre
            });

            //Assert
            modelId.Should().Be(resultModelId);
            modelName.Should().Be(resultModelName);
            modelPrice.Should().Be(resultModelPrice);
            modelDuration.Should().Be(resultModelDuration);
            modelArtistId.Should().Be(resultModelArtistId);
            modelGanre.Should().Be(resultModelGanre);

            successMessage.Should().Be($" Song {resultModelName} has been edited successfully");

            result.Should().BeOfType <RedirectToActionResult>();

            result.As <RedirectToActionResult>().ActionName.Should().Be("ListAll");
        }