public void Index() { // Arrange MoviesController movie = new MoviesController(); // Act ViewResult result = movie.Index() as ViewResult; // Assert Assert.IsNotNull(result); }
public void MovieStore_Index_TestView() { //Arrange MoviesController controller = new MoviesController(); //Act ViewResult result = controller.Index() as ViewResult; //Assert Assert.IsNotNull(result); }
public void Index_Test_TestView() { // Arrange MoviesController controller = new MoviesController(); // creating a basic controller object // Act ViewResult result = controller.Index() as ViewResult; //Assert Assert.IsNotNull(result); }
public void ShouldSearchTitles() { var sut = new MoviesController(new StubMovieRepository()); var result = sut.Index("", "Ghost") as ViewResult; Assert.IsNotNull(result); var moviesModel = (result.Model as IQueryable <Movie>).ToArray(); Assert.IsNotNull(moviesModel); Assert.AreEqual(2, moviesModel.Count()); }
public void IndexAction_ReturnsCorrectViewModel() { var moviesService = this.SetupMockMoviesService(); var usersService = this.SetupMockUsersService(); var mockUserManager = this.SetupMockUserManager(); var viewModelMock = new Mock <MovieSearchViewModel>(); var controller = new MoviesController(moviesService.Object, mockUserManager.Object, usersService.Object); var result = controller.Index(viewModelMock.Object) as ViewResult; Assert.IsInstanceOfType(result.Model, typeof(MovieSearchViewModel)); }
public void MovieReturnTypeTest() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseInMemoryDatabase(); var _dbContext = new ApplicationDbContext(optionsBuilder.Options); var controller = new MoviesController(_dbContext); var result = controller.Index(); Assert.IsType <Task <IActionResult> >(result); }
public void IndexAction_CallCorrectServiceMethod() { var moviesService = this.SetupMockMoviesService(); var usersService = this.SetupMockUsersService(); var mockUserManager = this.SetupMockUserManager(); var viewModelMock = new Mock <MovieSearchViewModel>(); var controller = new MoviesController(moviesService.Object, mockUserManager.Object, usersService.Object); var result = controller.Index(viewModelMock.Object) as ViewResult; moviesService.Verify(m => m.ListByContainingText(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once); moviesService.Verify(m => m.TotalContainingText(It.IsAny <string>()), Times.Once); }
public void IndexMethod_NoFilter_ShouldReturnAllMoviesInDb() { // Arrange _context.Movies.AddRange(_existingMovies); _context.SaveChanges(); // Act var indexResult = _controller.Index(null, null) as ViewResult; var moviesReturned = indexResult.Model as List <Movie>; // Assert Assert.AreEqual(2, moviesReturned.Count); }
public async Task Index_ReturnsGoodData() { // Arrange var controller = new MoviesController(null); // Act var result = await controller.Index("SF"); // Assert //var viewResult = Assert.IsType<ViewResult>(result); //var model = Assert.IsAssignableFrom<IEnumerable<StormSessionViewModel>>( // viewResult.ViewData.Model); //Assert.Equal(2, model.Count()); }
public void MovieStore_Index_TestView() { #region Arrange MoviesController controller = new MoviesController(); #endregion #region Act ViewResult result = controller.Index() as ViewResult; #endregion #region Assert Assert.IsNotNull(result); #endregion }
public void Index_ExactTitleSearch_YieldsResult(string titleName) { var result = _sut.Index(movieGenre: null, titleName) as ViewResult; var model = result.Model as IEnumerable <Movie>; model.Count().Should().Be(1); model.First().Title.Should().Be(titleName); }
public async Task MoviesController_Index_Test() { /// Arrange var controller = new MoviesController(_db); /// Act var result = await controller.Index("", "1"); /// Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <MovieGenreViewModel>(viewResult.ViewData.Model); Assert.Equal("PG", model.movies.FirstOrDefault().Genre); Assert.Equal(2, model.movies.Count); }
public void ReturnAllMoviesGivenEmptyStringParameters() { //Arange FakeMovieRepository repo = new FakeMovieRepository(); var moviesController = new MoviesController(repo); //Actual var result = moviesController.Index(String.Empty, String.Empty, String.Empty) as ViewResult; var movies = result.Model as IQueryable <Movie>; // Assert Assert.AreEqual(1, movies.Count()); Assert.AreEqual(true, repo.WasGetAllMoviesCalled); }
public void ReturnExpectedMoviesGivenGenere() { //Arrange FakeMovieRepository repo = new FakeMovieRepository(); var moviesController = new MoviesController(repo); //Actual var result = moviesController.Index("Comedy", String.Empty, String.Empty) as ViewResult; // returning null always :( on debigging found that calling fake repo function but returning null ! var movies = result.Model as IQueryable <Movie>; // Assert Assert.AreEqual(1, movies.Count()); Assert.AreEqual(true, repo.WasGetAllMoviesCalled); }
public void ReturnExpectedMoviesGivenSearchString() { //Arrange FakeMovieRepository repo = new FakeMovieRepository(); var moviesController = new MoviesController(repo); //Actual var result = moviesController.Index(String.Empty, "Rio", String.Empty) as ViewResult; var movies = result.Model as IQueryable <Movie>; var selectedmovie = (from r in movies where r.Title.Contains("Rio") select r).FirstOrDefault().Title; //Assert Assert.AreEqual(true, repo.WasGetAllMoviesCalled); Assert.AreEqual("Rio", selectedmovie); }
public void Index_ReturnsAViewResult_WithAListOfMovieSet() { //Arrange var mock = new Mock <IRepository>(); mock.Setup(repo => repo.List <MovieDTO>()).Returns(GetTestMovies()); var controller = new MoviesController(mock.Object, webHostEnvironment); //Act var result = controller.Index(null, null); //Assert var model = Assert.IsAssignableFrom <IEnumerable <MovieDTO> >(result.ViewData.Model); Assert.Equal(2, model.Count()); //Assert.IsType<ViewResult>(result); }
public void WriteAllMovies() { var movieDbContext = new MovieDBContext(); var controller = new MoviesController(movieDbContext); for (var i = 1; i <= NumberOfSearches; i++) { var result = controller.Index(movieGenre: null, searchString: null) as ViewResult; var model = result.Model as IEnumerable <Movie>; foreach (var movie in model) { Console.WriteLine($"Result {movie.Title}"); } } }
public void Movies_Index() { // Arrange Source source = GetTestSource(); var movies = GetTestMovieSet(source); var db = new Mock <IPersonalInformationManagerContext>(); db.Setup(e => e.Movies).Returns(movies); MoviesController controller = new MoviesController(db.Object); // Act ViewResult result = controller.Index("", "", "", null) as ViewResult; var model = result.Model as PagedList.IPagedList <PersonalInformationManager.Models.Movie>; // Assert Assert.IsNotNull(result); Assert.AreEqual(1, model.Count()); }
public async Task Should_Return_No_Movies_On_Index_Method_When_No_Movies_In_Database() { // Arrange MvcMovieContext context = new MvcMovieContextBuilder() .WithInMemoryProvider() .Context; MoviesRepository repository = new MoviesRepository(context); MoviesService service = new MoviesService(repository); MoviesController systemUnderTest = new MoviesController(service); // Act IActionResult result = await systemUnderTest.Index(string.Empty, string.Empty); // Assert Assert.True(result is ViewResult); Assert.True(((ViewResult)result).Model is MovieGenreViewModel); MovieGenreViewModel model = ((ViewResult)result).Model as MovieGenreViewModel; Assert.Equal(model.Movies.Count, ZERO_MOVIES); }
public async Task Index_ReturnsAViewResult_WithAListOf2Movies() { // Arrange (given) var mockRepo = new Mock <IMoviesRepository>(); mockRepo.Setup(repo => repo.GetMovies()) .ReturnsAsync(() => new List <Movie>() { new Movie(), new Movie() }); var controller = new MoviesController(mockRepo.Object); // Act (when) var result = await controller.Index(); // Assert (then) ViewResult viewResult = Assert.IsType <ViewResult>(result); IEnumerable <Movie> model = Assert.IsAssignableFrom <IEnumerable <Movie> >( viewResult.ViewData.Model); Assert.Equal(2, model.Count()); }
public void ReturnGenresInViewBag() { var expectedGenres = new List <string>() { "Action", "Horror", "Comedy" }; var fakeRepo = new FakeMovieRepository() { GenreList = expectedGenres }; var controller = new MoviesController(fakeRepo); var result = controller.Index("foo", "bar") as ViewResult; SelectList genreSelectList = result.ViewBag.movieGenre as SelectList; foreach (var genreItem in genreSelectList) { CollectionAssert.Contains(expectedGenres, genreItem.Text); } Assert.That(fakeRepo.WasListMoviesCalled); }
public void IndexTest() { // Arrange var movies = new List <Movie> { new Movie { Id = 1, Title = "Star Wars", Director = "George" } }; var mockRepository = new Mock <IRepository>(); mockRepository.Setup(r => r.ListMovies()).Returns(movies); var controller = new MoviesController(mockRepository.Object); // this is a workaround for a bug controller.ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()); // Act var result = (ViewResult)controller.Index(); var model = (IList <Movie>)result.ViewData.Model; // Assert Assert.Equal("Star Wars", model.First().Title); }
public async void TestHomeControllerAndCheckIndexForReturningList() { //Arrange var mock = new Mock <IMovieRepository>(); var list = new List <Movie>() { new Movie() { ID = 0, Name = "Hello", Rating = 9.2, ReleaseYear = 2012 }, new Movie() { ID = 1, Name = "bye", Rating = 0.2, ReleaseYear = 2010 } }; mock.Setup(frame => frame.MovieListAsync()).Returns(list); IMovieRepository repo = mock.Object; var controller = new MoviesController(repo); //Act var action = controller.Index(); //Assert Assert.IsType <ViewResult>(action); var result = action as ViewResult; var model = result.Model as ICollection <Movie>; Assert.Equal(2, model.Count); Assert.Equal(list, model); }
public async Task Index_ActionExecutes_ReturnsViewForIndex() { var result = await _controller.Index(); Assert.IsType <ViewResult>(result); }
public void IndexWithMoviesHasMovies() { // arrange // var db = new MoviesDBContext(); // in-memory // var repo = new MovieRepoDB(); // var controller = new MoviesController(); // at this point we are not really doing a unit test. // this is really more like an integration test. // to unit test something like MVC, that uses dependency injection, // or really in general, to unit test something with complex (or any) dependencies // we will use mocking (with the Moq framework) // if we didn't have a mocking framework, we would use fakes like this. // var fakeRepo = new FakeMovieRepo(); // var controller = new MoviesController(fakeRepo); // because I used dependency inversion (my controller depends on an interface, // not a concrete class) I am able to provide a different implementation // without breaking or changing the controller. // dependency injection is what you call it when a framework automatically constructs // objects requested (e.g. as constructor parameters) instead of YOUR objects // constructing them themselves. var data = new List <Movie> { new Movie { Id = 1, Title = "Star Wars" } }; var mockRepo = new Mock <IMovieRepo>(); mockRepo .Setup(repo => repo.GetAll()) .Returns(data); mockRepo //.Setup(repo => repo.GetById(It.IsAny<int>()) // mocking for any parameter .Setup(repo => repo.GetById(1)) // mocking for specific parameter .Returns(data[0]); // you mock the methods that you expect the SUT's (subject under test) code to call // you don't need to mock the other ones unless you're truly blind-testing // the implementation of the controller. // it's possible to setup a mock so it verifies that certain methods have been called // if you just want a method to be callable without throwing an exception mockRepo .Setup(repo => repo.CreateMovie(It.IsAny <Movie>())); // that method is now callable but won't do anything var controller = new MoviesController(mockRepo.Object); // act // i had to add package references Microsoft.AspNetCore.Mvc.ViewFeatures to do this ActionResult result = controller.Index(); // assert // simple way to cast - throws exception if failure //ViewResult viewResult = (ViewResult)result; // more self-documenting tests if you assert that the cast succeeds // this asserts that the cast succeeds and also returns the castd value. ViewResult viewResult = Assert.IsAssignableFrom <ViewResult>(result); var movies = Assert.IsAssignableFrom <IEnumerable <Movie> >(viewResult.Model); var moviesList = movies.ToList(); // many ways I could check that this list is correct Assert.Equal(data.Count, moviesList.Count); for (int i = 0; i < data.Count; i++) { Assert.Equal(data[i].Id, moviesList[i].Id); } // we've tested that given a repo providing one movie with ID 1, // the controller's Index methdo will return a View having as model, // a collection of one movie with ID 1. // this is a great test }