示例#1
0
        public void GetWatchListReturnsNoElementsForInvalidUser()
        {
            // Arrange
            var filmData1 = new FilmData
            {
                Title = "Title1",
                Genre = new List <GenreType> {
                    new GenreType {
                        Genre = Genre.Action
                    }
                },
                Poster = "Poster1",
                FilmId = "Film1"
            };
            var filmData2 = new FilmData
            {
                Title = "Title2",
                Genre = new List <GenreType> {
                    new GenreType {
                        Genre = Genre.Action
                    }
                },
                Poster = "Poster2",
                FilmId = "Film2"
            };
            var film1 = new Film
            {
                Rating   = 2,
                Id       = "Film1",
                FilmData = filmData1
            };
            var film2 = new Film
            {
                Rating   = 3,
                Id       = "Film2",
                FilmData = filmData2
            };

            filmData1.Film = film1;
            filmData2.Film = film2;
            var user = new CinemaUser
            {
                WatchList = new List <Film> {
                    film1, film2
                }
            };
            var users = new List <CinemaUser> {
                user
            }.AsQueryable();

            var mockSet = new Mock <DbSet <CinemaUser> >();

            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.Provider).Returns(users.Provider);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.Expression).Returns(users.Expression);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            var mockContext = new Mock <CinemaDbContext>();

            mockContext.Setup(c => c.Users).Returns(mockSet.Object);
            mockContext.Setup(s => s.Users.FindAsync(It.IsAny <string>())).
            Returns(new ValueTask <CinemaUser>(Task.FromResult((CinemaUser)null)));

            var filmBusiness      = new FilmBusiness(mockContext.Object);
            var mapper            = new Mapper();
            var expectedWatchList = new List <FilmCardViewModel>();

            // Act
            var resultWatchList = filmBusiness.GetWatchList("UnexistingUserId", mapper.MapToFilmCardViewModel);

            // Assert
            Assert.AreEqual(null, resultWatchList, "Incorrect watchlist returned");
        }
示例#2
0
        public void GetWatchListReturnsAllElements()
        {
            // Arrange
            var filmData1 = new FilmData
            {
                Title = "Title1",
                Genre = new List <GenreType> {
                    new GenreType {
                        Genre = Genre.Action
                    }
                },
                Poster = "Poster1",
                FilmId = "Film1"
            };
            var filmData2 = new FilmData
            {
                Title = "Title2",
                Genre = new List <GenreType> {
                    new GenreType {
                        Genre = Genre.Action
                    }
                },
                Poster = "Poster2",
                FilmId = "Film2"
            };
            var film1 = new Film
            {
                Rating   = 2,
                Id       = "Film1",
                FilmData = filmData1
            };
            var film2 = new Film
            {
                Rating   = 3,
                Id       = "Film2",
                FilmData = filmData2
            };

            filmData1.Film = film1;
            filmData2.Film = film2;
            var user = new CinemaUser {
                WatchList = new List <Film> {
                    film1, film2
                }
            };
            var users = new List <CinemaUser> {
                user
            }.AsQueryable();

            var mockSet = new Mock <DbSet <CinemaUser> >();

            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.Provider).Returns(users.Provider);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.Expression).Returns(users.Expression);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockSet.As <IQueryable <CinemaUser> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

            var mockContext = new Mock <CinemaDbContext>();

            mockContext.Setup(c => c.Users).Returns(mockSet.Object);
            mockContext.Setup(s => s.Users.FindAsync(It.IsAny <string>())).Returns(new ValueTask <CinemaUser>(user));

            var filmBusiness      = new FilmBusiness(mockContext.Object);
            var mapper            = new Mapper();
            var expectedWatchList = user.WatchList.Select(x => mapper.MapToFilmCardViewModel(x.FilmData)).ToList();

            // Act
            var resultWatchList = filmBusiness.GetWatchList(user.Id, mapper.MapToFilmCardViewModel).ToList();

            // Assert
            Assert.AreEqual(expectedWatchList.Count, resultWatchList.Count, "Incorrect watchlist returned");
            Assert.AreEqual(expectedWatchList[0].Id, resultWatchList[0].Id, "Incorect data returned");
            Assert.AreEqual(expectedWatchList[0].Poster, resultWatchList[0].Poster, "Incorect data returned");
            Assert.AreEqual(expectedWatchList[0].Rating, resultWatchList[0].Rating, "Incorect data returned");
            Assert.AreEqual(expectedWatchList[0].Title, resultWatchList[0].Title, "Incorect data returned");
            Assert.AreEqual(expectedWatchList[0].Genres, resultWatchList[0].Genres, "Incorect data returned");
            Assert.AreEqual(expectedWatchList[1].Id, resultWatchList[1].Id, "Incorect data returned");
        }