Ejemplo n.º 1
0
 public ActionResult Create(Playlist playlist)
 {
     playlist.ApplicationUserId = this.User.GetUserId();
     try
     {
         _playlistRepo.Create(playlist);
         _playlistRepo.SaveChanges();
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         return(View());
     }
 }
        public void Test1()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "MovieListDatabase")
                          .Options;

            var data = new List <Playlist>
            {
                new Playlist
                {
                    Id                = new Guid("D6439971-6030-4983-A531-08D8C6A0B4A2"),
                    PlaylistName      = "Just a playlist",
                    ApplicationUserId = "1265ace9-c141-4837-aab7-32eb45d1af2d"
                },
                new Playlist
                {
                    Id                = new Guid("2B363996-E8AA-4710-70CA-08D8C6CBFC5B"),
                    PlaylistName      = "Just a playlist",
                    ApplicationUserId = "937bcaf9-3a2a-4136-a5b6-f3c8c6945b55"
                }
            }.AsQueryable();
            var mockSet = new Mock <DbSet <Playlist> >();

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

            var userData = new List <ApplicationUser>
            {
                new ApplicationUser
                {
                    UserName = "******",
                    Id       = "1265ace9-c141-4837-aab7-32eb45d1af2d"
                },
                new ApplicationUser
                {
                    UserName = "******",
                    Id       = "937bcaf9-3a2a-4136-a5b6-f3c8c6945b55"
                }
            }.AsQueryable();
            var mockSetUser = new Mock <DbSet <ApplicationUser> >();

            mockSetUser.As <IQueryable <ApplicationUser> >().Setup(m => m.Provider).Returns(userData.Provider);
            mockSetUser.As <IQueryable <ApplicationUser> >().Setup(m => m.Expression).Returns(userData.Expression);
            mockSetUser.As <IQueryable <ApplicationUser> >().Setup(m => m.ElementType).Returns(userData.ElementType);
            mockSetUser.As <IQueryable <ApplicationUser> >().Setup(m => m.GetEnumerator()).Returns(userData.GetEnumerator());

            _dbContext = new Mock <ApplicationDbContext>(options);
            _dbContext.Setup(c => c.Playlists).Returns(mockSet.Object);
            _dbContext.Setup(c => c.Users).Returns(mockSetUser.Object);

            _playlistRepo = new PlaylistRepository(_dbContext.Object);

            _playlistRepo.Create(new Playlist
            {
                Id                = new Guid("CEEE9729-2B65-4292-7F8F-08D8C6D2FA9D"),
                PlaylistName      = "Just a playlist",
                ApplicationUserId = "1265ace9-c141-4837-aab7-32eb45d1af2d"
            });

            _playlistRepo.SaveChanges();

            Assert.Equal(2, _playlistRepo.GetAllFromUser("1265ace9-c141-4837-aab7-32eb45d1af2d").Count);
        }