Exemplo n.º 1
0
        public void Create_InvalidModel_ReturnsViewWithModel()
        {
            // Arrange
            var movieToCreate = new Movie();
            var modelBinder   = new ModelBindingContext()
            {
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(
                    () => movieToCreate, movieToCreate.GetType()),
                ValueProvider = new NameValueCollectionValueProvider(
                    new NameValueCollection(), CultureInfo.InvariantCulture)
            };
            var binder = new DefaultModelBinder().BindModel(
                new ControllerContext(), modelBinder);

            _controller.ModelState.Clear();
            _controller.ModelState.Merge(modelBinder.ModelState);

            // Act
            var result = _controller.Create(movieToCreate) as ViewResult;

            // Assert
            Assert.IsTrue(result.ViewData.ModelState["Genre"].Errors.Count > 0);
            Assert.IsTrue(result.ViewData.ModelState["Price"].Errors.Count > 0);
            Assert.IsFalse(result.ViewData.ModelState.IsValid);
        }
        public void MovieStore_Create_ModelStateIsValid()
        {
            #region Arrange
            var movies = new List <Movie>()
            {
                new Movie {
                    MovieId = 1, Title = "Superman 1", YearRelease = 2019
                },
                new Movie {
                    MovieId = 2, Title = "Superman 2", YearRelease = 2007
                }
            }.AsQueryable();

            Mock <MovieStoreDbContext> mockContext = new Mock <MovieStoreDbContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();

            mockSet.As <IQueryable <Movie> >().Setup(o => o.GetEnumerator())
            .Returns(movies.GetEnumerator());

            mockContext.Setup(o => o.Movies)
            .Returns(mockSet.Object);
            #endregion

            #region Act
            MoviesController controller = new MoviesController(mockContext.Object);

            RedirectToRouteResult result = controller.Create(movies.First()) as RedirectToRouteResult;
            #endregion

            #region Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
            #endregion
        }
Exemplo n.º 3
0
        public void MovieStore_Create_TestView()
        {
            MoviesController controller = new MoviesController();
            ViewResult       result     = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
        }
        public void MovieStore_Create_ValidModelState()
        {
            Mock <NewMovieStoreDbContext> mockContext = new Mock <NewMovieStoreDbContext>();
            Mock <DbSet <Movie> >         mockSet     = new Mock <DbSet <Movie> >();

            Movie movie = new Movie()
            {
                MovieId = 1, Title = "The Negotiator"
            };

            mockSet.Setup(m => m.Find(It.IsAny <Object>())).Returns(movie);

            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            //Arrange
            MoviesController controller = new MoviesController(mockContext.Object);

            controller.ModelState.Clear();

            //Act
            RedirectToRouteResult result = controller.Create(movie) as RedirectToRouteResult;

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public void MovieStore_CreatePOST_NotValid()
        {
            // Step 1
            Mock <MovieStoreDbContext> mockContext = new Mock <MovieStoreDbContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();

            // Step 2
            var movie = new Movie()
            {
                MovieId = 1, Title = "Superman 1", YearRelese = 2019
            };

            // Step 3
            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            // Arrange
            MoviesController controller = new MoviesController(mockContext.Object);

            // Act
            controller.ModelState.AddModelError("test", "test");
            ViewResult result = controller.Create(movie) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 6
0
        public void AddMovie_ValidData()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddMovie_ValidData")
                          .Options;
            MoviesController controller = new MoviesController(new ApplicationDbContext(options));

            var movie = new Movie
            {
                Description = "Movie Description",
                Genre       = Genre.Action,
                Rating      = 5.4,
                Title       = "Movie Title",
                Year        = 2019
            };

            ActionResult result = controller.Create(movie);


            using (var context = new ApplicationDbContext(options))
            {
                Assert.Equal(1, context.Movies.Count());
                Assert.Equal("Movie Title", context.Movies.Single().Title);
                Assert.IsType <RedirectToActionResult>(result);
            }
        }
Exemplo n.º 7
0
        public void MovieStore_Create_TestPost()
        {
            var list = new List <Movie>
            {
                new Movie {
                    MovieId = 1, Title = "Superman 1"
                },
                new Movie {
                    MovieId = 2, Title = "Superman 2"
                },
            }.AsQueryable();

            //2
            Mock <MovieStoreDBContext> mockContext = new Mock <MovieStoreDBContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();

            //3
            mockSet.As <IQueryable <Movie> >().Setup(m => m.GetEnumerator()).Returns(list.GetEnumerator());
            mockSet.As <IQueryable <Movie> >().Setup(m => m.Provider).Returns(list.Provider);
            mockSet.As <IQueryable <Movie> >().Setup(m => m.ElementType).Returns(list.ElementType);
            mockSet.Setup(m => m.Find(It.IsAny <Object>())).Returns(list.First());

            //4
            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            //Arrage
            MoviesController controller = new MoviesController(mockContext.Object);

            //Act
            ViewResult result = controller.Create() as ViewResult;

            //Assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 8
0
        public void MovieStore_EditPOST_Success()
        {
            // Step 1
            Mock <MovieStoreDBContext> mockContext = new Mock <MovieStoreDBContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();

            // Step 2
            var movie = new Movie()
            {
                MovieId     = 1,
                Title       = "Superman",
                YearRelease = 2018
            };

            // Step 3
            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            // Arrange
            MoviesController controller = new MoviesController(mockContext.Object);

            // Act
            RedirectToRouteResult result = controller.Create(movie) as RedirectToRouteResult;

            // Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
Exemplo n.º 9
0
        [InlineData("fourth one", 3)] // since this gets executed first, there would be 3 movies
        public void verifyAddMovie(String title, int id)
        {
            Movie movie = new Movie()
            {
                Title = title
            };
            var result = controller.Create(movie);

            // verify status is successful
            Assert.Equal("RanToCompletion", result.Status.ToString());


            // verify new ID is added
            Assert.Equal(id, movie.Id);
            Assert.Equal(title, movie.Title);
        }
        public void MovieStore_Delete_IntParam_Success()
        {
            //Arrange
            Mock <MovieStoreDbContext> mockContext = new Mock <MovieStoreDbContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();


            var list = new List <Movie>
            {
                new Movie {
                    MovieId = 1, Title = "Jaws"
                },
                new Movie {
                    MovieId = 2, Title = "Despicable Me"
                }
            }.AsQueryable();

            mockSet.As <IQueryable <Movie> >().Setup(m => m.Provider).Returns(list.Provider);
            mockSet.As <IQueryable <Movie> >().Setup(m => m.GetEnumerator()).Returns(list.GetEnumerator());
            mockSet.As <IQueryable <Movie> >().Setup(m => m.Expression).Returns(list.Expression);
            mockSet.As <IQueryable <Movie> >().Setup(m => m.ElementType).Returns(list.ElementType);
            mockSet.Setup(m => m.Find(It.IsAny <int>())).Returns(list.First());
            mockSet.Setup(m => m.Remove(It.IsAny <Movie>())).Returns(list.First());

            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            //Controller needs a mock object for Dependency Injection
            MoviesController controller = new MoviesController(mockContext.Object);

            //Act
            var result = controller.Create(list.First()) as RedirectToRouteResult;

            //Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
Exemplo n.º 11
0
        public InMemoryDatabaseContextTest()
        {
            controller = new MoviesController(DbAccess.getInMemoryDatabaseContext());

            // prep data
            Movie movie1 = new Movie()
            {
                Title = "Shrek"
            };
            Movie movie2 = new Movie()
            {
                Title = "Another"
            };

            controller.Create(movie1);
            controller.Create(movie2);
        }
Exemplo n.º 12
0
        public void MoviesControllerCreate()
        {
            var moviesController = new MoviesController(null, null);

            var result = moviesController.Create();

            Assert.IsNotNull(result);
        }
        public void MovieCreateisCreatedOnFake()
        {
            _moviesController = new MoviesController(_fakeMovieRepository);

            var expectedType = typeof(ActionResult);
            var value        = _moviesController.Create(new Movie());

            Assert.IsInstanceOfType(value, expectedType);
        }
        public void MovieCreateisCreated()
        {
            _moviesController = new MoviesController(_movieRepository);

            var expectedType = typeof(ViewResult);
            var value        = _moviesController.Create();

            Assert.IsInstanceOfType(value, expectedType);
        }
Exemplo n.º 15
0
        public void MovieStore_CreatePost()
        {
            // Arrange
            MoviesController controller = new MoviesController();
            // Act
            ViewResult result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
Exemplo n.º 16
0
        public void sqlLiteTests2()
        {
            MoviesController controller = new MoviesController(DbAccess.getSqlLiteDbContext());
            Movie            movie1     = new Movie()
            {
                Title = "Shrek"
            };
            Movie movie2 = new Movie()
            {
                Title = "Another"
            };
            var x = controller.Create(movie1);
            var y = controller.Create(movie2);

            //controller.Create(movie1);
            Assert.True(controller.MovieExists(1));
            //Assert.True(controller.MovieExists(2));
            //Assert.False(controller.MovieExists(10));
        }
Exemplo n.º 17
0
        public void Movie()
        {
            //Arrange
            var mockRepo   = new Mock <MvcMovieContext>();
            var controller = new MoviesController(mockRepo.Object);
            //Act
            IActionResult result = controller.Create();

            //Assert
            Assert.NotNull(result);
        }
Exemplo n.º 18
0
        public void TestMethod_Create()
        {
            // Arrange
            mockMovieRepo.Setup(p => p.Create(mockMovieList[0]));
            moviesController = new MoviesController(mockMovieRepo.Object, mockMovieHelper.Object);

            // Act
            moviesController.Create(mockMovieList[0]);

            // Assert
            mockMovieRepo.Verify(m => m.Create(mockMovieList[0]), Times.Once);
        }
        public void Create_with_empty_input_model_returns_BadRequest()
        {
            // Arrange
            var mockService = new Mock <IMovieService>();
            var sut         = new MoviesController(mockService.Object);

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

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Exemplo n.º 20
0
        public void AddingMovie_ValidMovie_ShouldIncrementCountOfMoviesInDb()
        {
            // Arrange
            var currentMovieCount = _dal.GetMovies().Count();

            // Act
            _controller.Create(_movieToCreate);

            // Assert
            var newMovieCount = _dal.GetMovies().Count();

            Assert.AreEqual(1, newMovieCount - currentMovieCount);
        }
        public void MovieStore_Create_ReturnsView()
        {
            #region Arrange
            MoviesController controller = new MoviesController();
            #endregion

            #region Act
            ViewResult result = controller.Create() as ViewResult;
            #endregion

            #region Assert
            Assert.IsNotNull(result);
            #endregion
        }
Exemplo n.º 22
0
        public void MovieStore_Create_ModelStateIsValid()
        {
            //Step 1
            var list = new List <Movie>
            {
                new Movie()
                {
                    MovieID = 1,
                    Title   = "Superman 1"
                },

                new Movie()
                {
                    MovieID = 2,
                    Title   = "Superman 2"
                }
            }.AsQueryable();

            //Step 2

            Mock <MovieStoreDbContext> mockContext = new Mock <MovieStoreDbContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();



            //Step 3
            mockSet.As <IQueryable <Movie> >().Setup(expression: m => m.GetEnumerator()).Returns(list.GetEnumerator());
            mockSet.As <IQueryable <Movie> >().Setup(expression: m => m.Provider).Returns(list.Provider);
            mockSet.As <IQueryable <Movie> >().Setup(expression: m => m.ElementType).Returns(list.ElementType);
            mockSet.Setup(expression: m => m.Find(It.IsAny <Object>())).Returns(list.First());


            //Step 4
            mockContext.Setup(expression: db => db.Movies).Returns(mockSet.Object);

            //Arrange

            MoviesController controller = new MoviesController(mockContext.Object);


            //Act

            ViewResult result = controller.Create() as ViewResult;


            //Assert

            Assert.IsNotNull(result);
        }
        public void Create_with_invalid_model_state_returns_Unprocessable_and_model_state()
        {
            // Arrange
            var mockService = new Mock <IMovieService>();
            var sut         = new MoviesController(mockService.Object);

            sut.ModelState.AddModelError("Title", "Title is required");

            // Act
            var result = sut.Create(new MovieInputModel());

            // Assert
            var unprocessableResult = Assert.IsType <UnprocessableObjectResult>(result);
            var modelState          = Assert.IsType <SerializableError>(unprocessableResult.Value);
        }
        public void Movies_Create_Get()
        {
            // Arrange
            var sources = GetTestSourceSet();
            var db      = new Mock <IPersonalInformationManagerContext>();

            db.Setup(e => e.Sources).Returns(sources);
            MoviesController controller = new MoviesController(db.Object);

            // Act
            ViewResult result = controller.Create() as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
        public async Task Should_Return_RedirectToAction_To_Index_On_Create_When_ModelState_Is_Ok()
        {
            // Arrange
            Mock <IMoviesService> mockService     = new Mock <IMoviesService>();
            MoviesController      systemUnderTest = new MoviesController(mockService.Object);
            Movie  aMovie             = new Movie();
            string expectedActionName = nameof(systemUnderTest.Index);

            // Act
            var result = await systemUnderTest.Create(aMovie);

            // Assert
            Assert.True(result is RedirectToActionResult);
            Assert.Equal(((RedirectToActionResult)result).ActionName, expectedActionName);
        }
        public async Task Should_Return_View_On_Create_When_ModelState_Has_Errors()
        {
            // Arrange
            Mock <IMoviesService> mockService     = new Mock <IMoviesService>();
            MoviesController      systemUnderTest = new MoviesController(mockService.Object);

            systemUnderTest.ModelState.AddModelError("Error", "Unit Test");
            Movie aMovie = new Movie();

            // Act
            var result = await systemUnderTest.Create(aMovie);

            // Assert
            Assert.True(result is ViewResult);
            Assert.True(((ViewResult)result).Model is Movie);
        }
Exemplo n.º 27
0
        public void MovieStore_Create_ModelStateIsInvalid()
        {
            // Arrange
            Movie            movie      = null;
            MoviesController controller = new MoviesController();

            controller.ModelState.AddModelError("Test", "Model state is forced to be invalid");

            // Act
            ActionResult result = controller.Create(movie);

            // Assert
            // TODO: Finish Assertion

            Assert.IsNotNull(result);
        }
Exemplo n.º 28
0
        public void AddMovie_InvalidData()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddMovie_InvalidData")
                          .Options;
            MoviesController controller = new MoviesController(new ApplicationDbContext(options));

            Movie movie = null;

            ActionResult result = controller.Create(movie);

            using (var context = new ApplicationDbContext(options))
            {
                Assert.Equal(0, context.Movies.Count());
            }
        }
Exemplo n.º 29
0
        public void MovieStore_Create_IsValid()
        {
            // Goal: Query from our own list instead of the database.

            // Step 1
            var list = new List <Movie>
            {
                new Movie()
                {
                    MovieId = 1, Title = "Superman 1"
                },
                new Movie()
                {
                    MovieId = 2, Title = "Superman 2"
                }
            }.AsQueryable();


            // Step 2
            Mock <MovieStoreDbContext> mockContext = new Mock <MovieStoreDbContext>();
            Mock <DbSet <Movie> >      mockSet     = new Mock <DbSet <Movie> >();

            // Step 3
            mockSet.As <IQueryable <Movie> >().Setup(m => m.GetEnumerator()).Returns(list.GetEnumerator());
            mockSet.As <IQueryable <Movie> >().Setup(m => m.Provider).Returns(list.Provider);
            mockSet.As <IQueryable <Movie> >().Setup(m => m.ElementType).Returns(list.ElementType);
            Movie movie = new Movie()
            {
                MovieId = 1, Title = "Avengers", YearRelease = 2016
            };

            mockSet.Setup(m => m.Find(It.IsAny <Object>())).Returns(movie);

            // Step 4
            mockContext.Setup(db => db.Movies).Returns(mockSet.Object);

            // Arrange
            MoviesController controller = new MoviesController(mockContext.Object);

            // Act
            RedirectToRouteResult result = controller.Create(movie) as RedirectToRouteResult;



            // Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public void Create_with_valid_model_calls_AddMovie_and_returns_CreatedAtRoute()
        {
            // Arrange
            var mockService = new Mock <IMovieService>();
            var sut         = new MoviesController(mockService.Object);

            // Act
            var result = sut.Create(new MovieInputModel());

            // Assert
            mockService.Verify(service =>
                               service.AddMovie(It.IsAny <Movie>()), Times.Once);

            var createAtRouteResult = Assert.IsType <CreatedAtRouteResult>(result);

            Assert.Equal(expected: "GetMovie", actual: createAtRouteResult.RouteName);
        }