상속: Microsoft.AspNetCore.Mvc.Controller
예제 #1
0
        public async Task Browse_ReturnsViewWithGenre()
        {
            // Arrange
            var genreName = "Genre 1";

            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
            CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);

            var controller = new StoreController()
            {
                DbContext = dbContext,
            };

            // Act
            var result = await controller.Browse(genreName);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType<Genre>(viewResult.ViewData.Model);
            Assert.Equal(genreName, viewModel.Name);
            Assert.NotNull(viewModel.Albums);
            Assert.Equal(3, viewModel.Albums.Count);
        }
예제 #2
0
        public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
        {
            // Arrange
            var controller = new StoreController(_serviceProvider.GetRequiredService<MusicStoreContext>());

            // Act
            var result = await controller.Browse(string.Empty);

            // Assert
            Assert.IsType<NotFoundResult>(result);
        }
예제 #3
0
        public async Task Details_ReturnsHttpNotFoundWhenNoAlbumData()
        {
            // Arrange
            var albumId    = int.MinValue;
            var controller = new StoreController(
                _fixture.Context,
                new TestAppSettings());

            // Act
            var result = await controller.Details(_fixture.ServiceProvider.GetRequiredService <IMemoryCache>(), albumId);

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }
예제 #4
0
        public async Task Browse_ReturnsHttpNotFoundWhenNoGenreData()
        {
            // Arrange
            var controller = new StoreController()
            {
                DbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(),
            };

            // Act
            var result = await controller.Browse(string.Empty);

            // Assert
            Assert.IsType <HttpNotFoundResult>(result);
        }
예제 #5
0
        public async Task Details_ReturnsHttpNotFoundWhenNoAlbumData()
        {
            // Arrange
            var albumId    = int.MinValue;
            var controller = new StoreController()
            {
                DbContext = _serviceProvider.GetRequiredService <MusicStoreContext>(),
                Cache     = _serviceProvider.GetRequiredService <IMemoryCache>(),
            };

            // Act
            var result = await controller.Details(albumId);

            // Assert
            Assert.IsType <HttpNotFoundResult>(result);
        }
예제 #6
0
        public async Task Index_CreatesViewWithGenres()
        {
            // Arrange
            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
            CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);

            var controller = new StoreController(dbContext);

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType<List<Genre>>(viewResult.ViewData.Model);
            Assert.Equal(10, viewModel.Count);
        }
예제 #7
0
        public async Task Index_CreatesViewWithGenres()
        {
            // Arrange
            var dbContext = _fixture.Context;

            CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);

            var controller = new StoreController(dbContext, new TestAppSettings());

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType <List <Genre> >(viewResult.ViewData.Model);

            Assert.Equal(10, viewModel.Count);
        }
예제 #8
0
        public async Task Index_CreatesViewWithGenres()
        {
            // Arrange
            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();

            CreateTestGenres(numberOfGenres: 10, numberOfAlbums: 1, dbContext: dbContext);

            var controller = new StoreController(dbContext);

            // Act
            var result = await controller.Index();

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType <List <Genre> >(viewResult.ViewData.Model);

            Assert.Equal(10, viewModel.Count);
        }
예제 #9
0
        public async Task Details_ReturnsAlbumDetail()
        {
            // Arrange
            var albumId = 1;

            var dbContext = _serviceProvider.GetRequiredService <MusicStoreContext>();
            var genres    = CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);

            var cache = _serviceProvider.GetRequiredService <IMemoryCache>();

            var controller = new StoreController()
            {
                DbContext = dbContext,
                Cache     = cache,
            };

            // Act
            var result = await controller.Details(albumId);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType <Album>(viewResult.ViewData.Model);

            Assert.NotNull(viewModel.Genre);
            var genre = genres.SingleOrDefault(g => g.GenreId == viewModel.GenreId);

            Assert.NotNull(genre);
            Assert.NotNull(genre.Albums.SingleOrDefault(a => a.AlbumId == albumId));
            Assert.Null(viewModel.Artist);

            var cachedAlbum = cache.Get <Album>("album_1");

            Assert.NotNull(cachedAlbum);
            Assert.Equal(albumId, cachedAlbum.AlbumId);
        }
예제 #10
0
        public async Task Details_ReturnsHttpNotFoundWhenNoAlbumData()
        {
            // Arrange
            var albumId = int.MinValue;
            var controller = new StoreController()
            {
                DbContext = _serviceProvider.GetRequiredService<MusicStoreContext>(),
                Cache = _serviceProvider.GetRequiredService<IMemoryCache>(),
            };

            // Act
            var result = await controller.Details(albumId);

            // Assert
            Assert.IsType<HttpNotFoundResult>(result);
        }
예제 #11
0
        public async Task Details_ReturnsAlbumDetail()
        {
            // Arrange
            var albumId = 1;

            var dbContext = _serviceProvider.GetRequiredService<MusicStoreContext>();
            var genres = CreateTestGenres(numberOfGenres: 3, numberOfAlbums: 3, dbContext: dbContext);

            var cache = _serviceProvider.GetRequiredService<IMemoryCache>();

            var controller = new StoreController()
            {
                DbContext = dbContext,
                Cache = cache,
            };

            // Act
            var result = await controller.Details(albumId);

            // Assert
            var viewResult = Assert.IsType<ViewResult>(result);
            Assert.Null(viewResult.ViewName);

            Assert.NotNull(viewResult.ViewData);
            var viewModel = Assert.IsType<Album>(viewResult.ViewData.Model);
            Assert.NotNull(viewModel.Genre);
            var genre = genres.SingleOrDefault(g => g.GenreId == viewModel.GenreId);
            Assert.NotNull(genre);
            Assert.NotNull(genre.Albums.SingleOrDefault(a => a.AlbumId == albumId));
            Assert.Null(viewModel.Artist);

            var cachedAlbum = cache.Get<Album>("album_1");
            Assert.NotNull(cachedAlbum);
            Assert.Equal(albumId, cachedAlbum.AlbumId);
        }