コード例 #1
0
        public void UpdateBook_ExistingBook_ReturnsGetBookDto()
        {
            AddBookDto newBook = BookTestData.AddBookDto();

            mapperMock.Setup(m => m.Map <Book>(It.IsAny <AddBookDto>())).Returns(BookTestData.Book());
            mapperMock.Setup(m => m.Map <UpdateBookDto>(It.IsAny <GetBookDto>())).Returns(BookTestData.AddBookDtoToUpdateBookDtoMapping());
            mapperMock.Setup(m => m.Map <GetBookDto>(It.IsAny <Book>())).Returns(BookTestData.AddBookDtoToGetBookDtoMapping());

            BookService bookService = new BookService(mapperMock.Object, dbContextMock.Object);

            ServiceResponse <List <GetBookDto> > addBookResponse = bookService.AddBook(newBook);
            GetBookDto addedBook = addBookResponse.Data.First();

            UpdateBookDto updatedBook = mapperMock.Object.Map <UpdateBookDto>(addedBook);

            updatedBook.Price = 900;

            ServiceResponse <GetBookDto> updatedBookResponse = bookService.UpdateBook(updatedBook);
            GetBookDto savedBook = updatedBookResponse.Data;

            Assert.That(updatedBookResponse.Success, Is.EqualTo(true));
            Assert.That(updatedBookResponse, Is.InstanceOf <ServiceResponse <GetBookDto> >());
            Assert.That(savedBook, Is.InstanceOf <GetBookDto>());
            Assert.That(savedBook.Price, Is.EqualTo(updatedBook.Price));
        }
コード例 #2
0
        public void DeleteBook_NonExistingBook_ReturnsNotFound()
        {
            mockService.Setup(s => s.DeleteBook(It.IsAny <int>()))
            .Returns(BookTestData.DeleteBookServiceResponseNullData());

            BookController bookController = new BookController(mockService.Object);

            var result = bookController.DeleteBook(1);

            Assert.That(result, Is.InstanceOf <NotFoundObjectResult>());
        }
コード例 #3
0
        public void UpdateBook_NonExistingBook_ReturnsNotFound()
        {
            mockService.Setup(s => s.UpdateBook(It.IsAny <UpdateBookDto>()))
            .Returns(BookTestData.UpdateBookServiceResponseNullData());

            BookController bookController = new BookController(mockService.Object);

            UpdateBookDto updatedBook = BookTestData.UpdateBookDto();
            var           result      = bookController.UpdateBook(updatedBook);

            Assert.That(result, Is.InstanceOf <NotFoundObjectResult>());
        }
コード例 #4
0
        public void AddBook_NewBook_ReturnsOk()
        {
            mockService.Setup(s => s.AddBook(It.IsAny <AddBookDto>()))
            .Returns(BookTestData.AddBookServiceResponse());

            BookController bookController = new BookController(mockService.Object);
            AddBookDto     newBook        = BookTestData.AddBookDto();

            var result = bookController.AddBook(newBook);

            Assert.That(result, Is.InstanceOf <OkObjectResult>());
        }
コード例 #5
0
        public void Setup()
        {
            mapperMock = new Mock <IMapper>();

            List <Book> books = new List <Book>
            {
                BookTestData.BookDummy()
            };

            dbContextMock = new Mock <IDataContext>();
            dbContextMock.Setup(p => p.Books).Returns(DbContextMock.GetQueryableMockDbSet <Book>(books));
            dbContextMock.Setup(p => p.SaveChanges()).Returns(1);
        }
コード例 #6
0
        public void UpdateBook_NonExistingBook_ReturnsError()
        {
            UpdateBookDto updatedBook = BookTestData.UpdateBookDto();

            BookService bookService = new BookService(mapperMock.Object, dbContextMock.Object);

            ServiceResponse <GetBookDto> updatedBookResponse = bookService.UpdateBook(updatedBook);
            GetBookDto savedBook = updatedBookResponse.Data;

            Assert.That(updatedBookResponse.Success, Is.EqualTo(false));
            Assert.That(updatedBookResponse, Is.InstanceOf <ServiceResponse <GetBookDto> >());
            Assert.That(updatedBookResponse.Data, Is.EqualTo(null));
        }
コード例 #7
0
        public void GetAllBooks_ReturnsOk()
        {
            mockService.Setup(s => s.GetAllBooks())
            .Returns(BookTestData.GetAllBooksServiceResponse());

            BookController bookController = new BookController(mockService.Object);
            AddBookDto     newBook        = BookTestData.AddBookDto();

            bookController.AddBook(newBook);

            var result = bookController.Get();

            Assert.That(result, Is.InstanceOf <OkObjectResult>());
        }
コード例 #8
0
        public void GetBookById_ExistingBook_ReturnsOk()
        {
            mockService.Setup(s => s.AddBook(It.IsAny <AddBookDto>()))
            .Returns(BookTestData.AddBookServiceResponse());
            mockService.Setup(s => s.GetBookById(It.IsAny <int>()))
            .Returns(BookTestData.GetSingleBookServiceResponse());

            BookController bookController = new BookController(mockService.Object);
            AddBookDto     newBook        = BookTestData.AddBookDto();

            bookController.AddBook(newBook);

            var result = bookController.GetSingleBook(1);

            Assert.That(result, Is.InstanceOf <OkObjectResult>());
        }
コード例 #9
0
        public void DeleteBook_ExistingBook_ReturnsGetBookDtoList()
        {
            AddBookDto newBook = BookTestData.AddBookDto();

            mapperMock.Setup(m => m.Map <Book>(It.IsAny <AddBookDto>())).Returns(BookTestData.Book());
            mapperMock.Setup(m => m.Map <GetBookDto>(It.IsAny <Book>())).Returns(BookTestData.BookMapping());

            BookService bookService = new BookService(mapperMock.Object, dbContextMock.Object);

            ServiceResponse <List <GetBookDto> > addBookResponse = bookService.AddBook(newBook);
            GetBookDto addedBook = addBookResponse.Data.First();

            ServiceResponse <List <GetBookDto> > deleteBookResponse = bookService.DeleteBook(addedBook.Id);

            Assert.That(deleteBookResponse.Success, Is.EqualTo(true));
            Assert.That(deleteBookResponse, Is.InstanceOf <ServiceResponse <List <GetBookDto> > >());
            Assert.That(deleteBookResponse.Data, Is.InstanceOf <List <GetBookDto> >());
        }
コード例 #10
0
        public void AddBook_NewBook_ReturnsGetBookDtoList()
        {
            mapperMock.Setup(m => m.Map <Book>(It.IsAny <AddBookDto>())).Returns(BookTestData.Book());
            mapperMock.Setup(m => m.Map <GetBookDto>(It.IsAny <Book>())).Returns(BookTestData.GetBookDto());

            AddBookDto newBook = BookTestData.AddBookDto();

            BookService bookService = new BookService(mapperMock.Object, dbContextMock.Object);

            ServiceResponse <List <GetBookDto> > addBookResponse = bookService.AddBook(newBook);
            GetBookDto savedBook = addBookResponse.Data.First();

            Assert.That(addBookResponse.Success, Is.EqualTo(true));
            Assert.That(addBookResponse, Is.InstanceOf <ServiceResponse <List <GetBookDto> > >());
            Assert.That(addBookResponse.Data, Is.InstanceOf <List <GetBookDto> >());
            Assert.That(addBookResponse.Data.Count, Is.EqualTo(2));
            Assert.That(savedBook, Is.InstanceOf <GetBookDto>());
            Assert.That(savedBook.Name, Is.EqualTo(newBook.Name));
            Assert.That(savedBook.Author, Is.EqualTo(newBook.Author));
        }
コード例 #11
0
        public void GetAllBooks_GetAll_ReturnsGetBookDtoList()
        {
            AddBookDto firstBook  = BookTestData.AddBookDto();
            AddBookDto secondBook = BookTestData.SecondAddBookDto();

            mapperMock.Setup(m => m.Map <Book>(firstBook)).Returns(BookTestData.Book());
            mapperMock.Setup(m => m.Map <Book>(secondBook)).Returns(BookTestData.SecondAddBookDtoMapping());
            mapperMock.Setup(m => m.Map <GetBookDto>(BookTestData.Book())).Returns(BookTestData.BookMapping());
            mapperMock.Setup(m => m.Map <GetBookDto>(BookTestData.SecondAddBookDtoMapping())).Returns(BookTestData.SecondAddBookDtoToGetBookDtoMapping());

            BookService bookService = new BookService(mapperMock.Object, dbContextMock.Object);

            bookService.AddBook(firstBook);
            bookService.AddBook(secondBook);

            ServiceResponse <List <GetBookDto> > getAllBooksResponse = bookService.GetAllBooks();

            Assert.That(getAllBooksResponse.Success, Is.EqualTo(true));
            Assert.That(getAllBooksResponse, Is.InstanceOf <ServiceResponse <List <GetBookDto> > >());
            Assert.That(getAllBooksResponse.Data, Is.InstanceOf <List <GetBookDto> >());
            Assert.That(getAllBooksResponse.Data.Count, Is.EqualTo(3));
        }