public void Delete_Removes_Album()
        {
            // arrange
            var albumId      = 1;
            var deletedAlbum = new Album(albumId, "First Album", "columbia records", "img", 1, "Here is a comment", 5);
            var albumList    = new List <Album>()
            {
                deletedAlbum,
                new Album(2, "Second Album", "columbia records", "img", 1, "Here is a comment", 5)
            };

            // our controller's Delete() action is dependent on the Repository's
            // GetById(), Delete(), and GetAll() actions- they all need to be mocked
            albumRepo.GetById(albumId).Returns(deletedAlbum);
            albumRepo.When(d => d.Delete(deletedAlbum))
            .Do(d => albumList.Remove(deletedAlbum));
            albumRepo.GetAll().Returns(albumList);


            // act
            var result = underTest.DeleteAlbum(albumId);

            // assert
            // Below is an alternative to Assert.DoesNotContain(deletedTodo, result),
            // which does not work in all cases
            Assert.All(result, item => Assert.Contains("Second Album", item.Title));
        }
Beispiel #2
0
        public async Task DeleteAlbum()
        {
            // Arrange
            var seedIds = new List <int> {
                new Random().Next(1, 50), new Random().Next(51, 100)
            };
            string            expectedUserName  = string.Format("test_{0}@gmail.com", Guid.NewGuid());
            ControllerContext controllerContext = Utilities.SetupCurrentUserForController(expectedUserName);
            var    seedAlbums          = SeedAlbums(seedIds, expectedUserName);
            string seed                = Guid.NewGuid().ToString();
            var    mockAlbumRepository = new Mock <IAlbumRepository>();
            var    mockUserRepository  = new Mock <IUserRepository>();
            User   expectedUser        = new User()
            {
                Id       = seed,
                UserName = expectedUserName
            };

            mockUserRepository.Setup(m => m.GetOrAdd(It.IsAny <User>())).Returns(expectedUser);
            var mockUnitOfWork          = new Mock <IUnitOfWork>();
            AlbumsController controller = new AlbumsController(this._mapper, mockAlbumRepository.Object, mockUserRepository.Object, mockUnitOfWork.Object);

            controller.ControllerContext = controllerContext;
            foreach (var seedAlbum in seedAlbums)
            {
                var id = seedAlbum.Id;
                mockAlbumRepository.Setup(m => m.GetAsync(id, true)).ReturnsAsync(seedAlbum);
                // Act
                var result = await controller.DeleteAlbum(id);

                // Assert
                Assert.IsType <OkResult>(result);
                mockAlbumRepository.Verify(m => m.Delete(seedAlbum), Times.Once);
            }
        }