Beispiel #1
0
        public void CallAddMethodOfGenreService_WhenModelStateIsValid()
        {
            var genreServiceMock  = new Mock <IGenreService>();
            var movieServiceMock  = new Mock <IMovieService>();
            var personServiceMock = new Mock <IPersonService>();
            var fileConverterMock = new Mock <IFileConverter>();
            var mapperMock        = new Mock <IMapper>();

            var genreViewModel = new AddGenreViewModel()
            {
                Name = "Genre"
            };

            var genreDbModel = new Genre()
            {
                Name = genreViewModel.Name
            };

            var validationContext =
                new System.ComponentModel.DataAnnotations.ValidationContext(genreViewModel, null, null);

            var results = new List <ValidationResult>();

            var isModelValid = Validator.TryValidateObject(genreViewModel, validationContext, results);

            mapperMock.Setup(x => x.Map <Genre>(genreViewModel)).Returns(genreDbModel);

            var panelController = new PanelController(
                genreServiceMock.Object,
                movieServiceMock.Object,
                personServiceMock.Object,
                fileConverterMock.Object,
                mapperMock.Object);

            // Act
            panelController.AddGenre(genreViewModel);

            // Assert
            Assert.IsTrue(isModelValid);
            genreServiceMock.Verify(gs => gs.AddGenre(genreDbModel), Times.Once);
        }
Beispiel #2
0
        public void NotCallAddMethodOfGenreService_WhenModelStateIsInvalid()
        {
            var genreServiceMock  = new Mock <IGenreService>();
            var movieServiceMock  = new Mock <IMovieService>();
            var personServiceMock = new Mock <IPersonService>();
            var fileConverterMock = new Mock <IFileConverter>();
            var mapperMock        = new Mock <IMapper>();

            var genreViewModel = new AddGenreViewModel()
            {
            };

            var genreDbModel = new Genre()
            {
            };

            var validationContext =
                new System.ComponentModel.DataAnnotations.ValidationContext(genreViewModel, null, null);

            var results = new List <ValidationResult>();

            var isModelValid = Validator.TryValidateObject(genreViewModel, validationContext, results);

            var panelController = new PanelController(
                genreServiceMock.Object,
                movieServiceMock.Object,
                personServiceMock.Object,
                fileConverterMock.Object,
                mapperMock.Object);

            panelController.ModelState.AddModelError("name", "No genre name!");

            // Act
            panelController.AddGenre(genreViewModel);

            // Assert
            Assert.IsFalse(isModelValid);
            genreServiceMock.Verify(gs => gs.AddGenre(genreDbModel), Times.Never);
        }