Пример #1
0
        public async Task Get_Returns_Empty_200_Empty_Listing()
        {
            // Arrange
            var logger = new Mock <ILogger <BooksController> >();

            var bookRepository = new Mock <IBookRepository>();

            var context = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "test_name_identifier") }) })
                }
            };

            var booksController = new BooksController(logger.Object, bookRepository.Object);

            booksController.ControllerContext = context;

            // Act
            var getResponse = await booksController.GetAsync();

            // Assert
            var okResult    = Assert.IsAssignableFrom <OkObjectResult>(getResponse);
            var bookListing = Assert.IsAssignableFrom <IEnumerable <BookListingItemDto> >(okResult.Value);

            Assert.Empty(bookListing);
        }
Пример #2
0
        public async Task Get_Returns_404()
        {
            // Arrange
            var logger = new Mock <ILogger <BooksController> >();

            var bookRepository = new Mock <IBookRepository>();

            bookRepository.Setup(s => s.GetUserBookAsync("SOMEUSER", "IDDQ1")).Returns(Task.FromResult((BookServiceModel)null));

            var context = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "test_name_identifier") }) })
                }
            };

            var booksController = new BooksController(logger.Object, bookRepository.Object);

            booksController.ControllerContext = context;

            // Act
            var getResponse = await booksController.GetAsync("IDDQ1");

            // Assert
            Assert.IsType <NotFoundResult>(getResponse);
        }
        public async Task GetAsync_With_NotExistingId_Returns_NotFound()
        {
            //Arrange
            var booksController = new BooksController(_context);

            //Act
            var result = (NotFoundResult)await booksController.GetAsync(99);

            //Assert
            Assert.AreEqual(StatusCodes.Status404NotFound, result.StatusCode);
        }
Пример #4
0
        public async Task GetAsync_ReturnBook()
        {
            var bookDto = new BookResponseDto();
            var bookId  = Guid.NewGuid();

            _booksService.Setup(bs => bs.GetAsync(bookId)).ReturnsAsync(bookDto);

            var result = await _booksController.GetAsync(bookId);

            Assert.That(result.Value, Is.EqualTo(bookDto));
            _booksService.Verify(bs => bs.GetAsync(It.IsAny <Guid>()));
        }
        public async Task GetAsync_Returns_Collection()
        {
            //Arrange
            var booksController = new BooksController(_context);
            var existingCount   = _context.Books.Count();

            //Act
            var result = await booksController.GetAsync();

            //Assert
            Assert.AreEqual(existingCount, result.Count);
        }
        public async Task GetAsync_With_Id_Returns_OK_With_Found_Book()
        {
            //Arrange
            var booksController = new BooksController(_context);
            var firstItem       = _context.Books.FirstOrDefault();

            //Act
            var result = (ObjectResult)await booksController.GetAsync(firstItem.Id);

            //Assert
            Assert.IsNotNull(result.Value);
            Assert.AreEqual(firstItem.Title, ((Book)result.Value).Title);
            Assert.AreEqual(StatusCodes.Status200OK, result.StatusCode);
        }
Пример #7
0
        public async Task Get_Returns_200_NonEmpty_Listing()
        {
            // Arrange
            var logger = new Mock <ILogger <BooksController> >();

            var bookRepository = new Mock <IBookRepository>();

            bookRepository.Setup(s => s.GetBooksAsync(It.IsAny <string>())).Returns(Task.FromResult((IEnumerable <BookListingItemDto>) new[] {
                new BookListingItemDto {
                    BookId = "IDDQ1", Name = "The Bible"
                },
                new BookListingItemDto {
                    BookId = "IDDOG2", Name = "Service Manual for CAT CV-5b Forklift"
                }
            }));

            var context = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "test_name_identifier") }) })
                }
            };

            var booksController = new BooksController(logger.Object, bookRepository.Object);

            booksController.ControllerContext = context;

            // Act
            var getResponse = await booksController.GetAsync();

            // Assert
            var okResult    = Assert.IsType <OkObjectResult>(getResponse);
            var bookListing = Assert.IsAssignableFrom <IEnumerable <BookListingItemDto> >(okResult.Value);

            Assert.Equal(2, bookListing.Count());
            Assert.Contains(bookListing, b => b.BookId == "IDDQ1" && b.Name == "The Bible" && b.SubscribedToBook == false);
            Assert.Contains(bookListing, b => b.BookId == "IDDOG2" && b.Name == "Service Manual for CAT CV-5b Forklift" && b.SubscribedToBook == false);
        }
Пример #8
0
        public async Task Get_Returns_200_Book()
        {
            // Arrange
            var logger         = new Mock <ILogger <BooksController> >();
            var bookRepository = new Mock <IBookRepository>();

            bookRepository.Setup(s => s.GetUserBookAsync(It.IsAny <string>(), It.IsAny <string>())).Returns(
                Task.FromResult(new BookServiceModel {
                BookId = "IDDQ1", Name = "The Bible", Price = 666, Text = "Jibba jabba  complete text"
            })
                );

            var context = new ControllerContext
            {
                HttpContext = new DefaultHttpContext {
                    User = new ClaimsPrincipal(new[] { new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "test_name_identifier") }) })
                }
            };

            var booksController = new BooksController(logger.Object, bookRepository.Object);

            booksController.ControllerContext = context;

            // Act
            var getResponse = await booksController.GetAsync("IDDQ1");

            // Assert
            var okResult = Assert.IsType <OkObjectResult>(getResponse);
            var book     = Assert.IsType <BookDto>(okResult.Value);

            Assert.NotNull(book);
            Assert.Equal("IDDQ1", book.BookId);
            Assert.Equal("The Bible", book.Name);
            Assert.Equal(666.0, book.Price);
            Assert.Equal("Jibba jabba  complete text", book.Text);
        }