Ejemplo n.º 1
0
        public async Task AddToWatchListAsyncDoesntAddForUnexistingUser()
        {
            // Arrange
            var film = new Film {
                Rating = 2
            };
            var users = new List <CinemaUser>
            {
                new CinemaUser {
                    Id = "TestUserId"
                }
            }.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>((CinemaUser)null));

            var filmBusiness = new FilmBusiness(mockContext.Object);

            // Act
            await filmBusiness.AddToWatchListAsync("UnexistingUserId", film);

            // Assert
            Assert.AreEqual(0, mockSet.Object.First().WatchList.Count(), "Film added to watchlist for unexising user");
            mockContext.Verify(m => m.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Never());
        }
Ejemplo n.º 2
0
        public async Task AddToWatchListAsyncAddsTheFilm()
        {
            // Arrange
            var email    = "*****@*****.**";
            var username = "******";
            var user     = new CinemaUser
            {
                ConcurrencyStamp   = Guid.NewGuid().ToString(),
                Email              = email,
                NormalizedEmail    = email.ToUpper(),
                EmailConfirmed     = true,
                UserName           = username,
                NormalizedUserName = username.ToUpper(),
                FirstName          = username,
                LastName           = username,
                Gender             = GenderType.Male,
                SecurityStamp      = Guid.NewGuid().ToString(),
                WatchList          = new List <Film> {
                    new Film()
                }
            };
            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 film = new Film {
                Rating = 2
            };

            // Act
            await filmBusiness.AddToWatchListAsync(user.Id, film);

            // Assert
            Assert.AreEqual(2, mockSet.Object.First().WatchList.Count(), "Film not added to watchlist");
            mockContext.Verify(m => m.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once());
        }
Ejemplo n.º 3
0
        public async Task AddToWatchListAsyncDoesntAddAlredyAddedFilm()
        {
            // Arrange
            var film = new Film {
                Rating = 2
            };
            var user = new CinemaUser {
                WatchList = new List <Film> {
                    film
                }
            };
            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);

            // Act
            await filmBusiness.AddToWatchListAsync(user.Id, film);

            // Assert
            Assert.AreEqual(1, mockSet.Object.First().WatchList.Count(), "Film added to watchlist when already exists");
            mockContext.Verify(m => m.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Never());
        }