コード例 #1
0
        public async Task GetCheckedOutBooksServiceCallsGetCheckoutBooksRepository()
        {
            var bookServiceMock = new Mock<IBookRepository>();
            bookServiceMock.Setup(b => b.GetCheckedOutBooks(It.IsAny<string>()))
                .ReturnsAsync(new List<CheckedOutBookDTO>());

            var userServiceMock = new Mock<IUserService>();

            var bookService = new BooksService(bookServiceMock.Object, userServiceMock.Object);

            userServiceMock.Setup(s => s.UserId).Returns("test");
            var result = await bookService.GetCheckedOutBooks();

            bookServiceMock.Verify(v => v.GetCheckedOutBooks("test"), Times.Once);
        }
コード例 #2
0
        public async Task GetCheckedOutBooksServiceReturnsViewModel()
        {
            var bookRepositoryMock = new Mock<IBookRepository>();
            bookRepositoryMock.Setup(b => b.GetCheckedOutBooks(It.IsAny<string>()))
                .ReturnsAsync(new List<CheckedOutBookDTO>
                {
                    new CheckedOutBookDTO()
                    {
                        Author = "test",
                        BookId = 1,
                        PhysicalBookId = 2,
                        State = CheckedOutBookState.Success,
                        Title = "test"
                    }
                });
            var expected = new CheckedOutBookViewModel()
            {
                Author = "test",
                BookId = 1,
                Title = "test",
                UserName = "******"
            };

            var userServiceMock = new Mock<IUserService>();

            userServiceMock.Setup(s => s.UserId).Returns("test");
            userServiceMock.Setup(s => s.UserName).Returns("test");

            var bookService = new BooksService(bookRepositoryMock.Object, userServiceMock.Object);

            var result = await bookService.GetCheckedOutBooks();
            var resultItem = result.First();

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(expected.Author, resultItem.Author);
            Assert.AreEqual(expected.BookId, resultItem.BookId);
            Assert.AreEqual(expected.Title, resultItem.Title);
            Assert.AreEqual(expected.UserName, resultItem.UserName);
        }