コード例 #1
0
        public void RegisterBook_ShouldInvokeRegisterOnStoreWhenEmpty()
        {
            var store = new Mock<IBibliotecaStore>();
            store.Setup(x => x.AddItem(It.IsAny<Book>())).Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Register(new Book() { ID = "ABC", Name = "Test Book" });

            store.Verify();
        }
コード例 #2
0
        static void test()
        {
            BibliotecaService bibliotecaService = BibliotecaServiceFactory.Crear();

            // Crear un nuevo libro
            bibliotecaService.AgregarTituloLibro("978-0321832054", "RoR Tutorial");

            // Cargar un nuevo ejemplar
            bibliotecaService.AgregarLibro("978-0321832054");
            // Cargar un nuevo ejemplar
            bibliotecaService.AgregarLibro("978-0321832054");
        }
コード例 #3
0
        public void RegisterBook_ShouldNotRegisterAlreadyExistingBook()
        {
            var store = new Mock<IBibliotecaStore>();
            store
                .Setup(x => x.GetItem(It.Is<string>(i=>i == "HEA1")))
                .Returns(this.bookCatalog
                    .Where(b => b.ID == "HEA1")
                    .FirstOrDefault());

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Register(new Book() { ID = "HEA1", Name = "Head First Design Patterns" });          
        }
コード例 #4
0
        private static void InitializeDependencies()
        {
            var users = new List<User>();
            users.Add(new User() { Username = "******" });
            users.Add(new User() { Username = "******" });

            var bibliotecaStore = new InMemoryBibliotecaStore(users);
            bibliotecaService = new BibliotecaService(bibliotecaStore);
        }
コード例 #5
0
        public void Lend_ShouldThrowExceptionWhenBookIsNotInCatalog()
        {
            var store = new Mock<IBibliotecaStore>();
            store.Setup(x => x.GetItem(It.Is<string>(i => i == "abcd"))).Returns((LibraryItem)null);      

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Lend("abcd", "edin", DateTime.Now);            
        }
コード例 #6
0
        public void RegisterBook_ShouldInvokeRegisterOnStoreWhenNonEmpty()
        {
            var store = new Mock<IBibliotecaStore>();
            store
                .Setup(x => x.GetItem(It.Is<string>(i => i == "ABC"))).Returns( (LibraryItem) null);                
            store.Setup(x => x.AddItem(It.IsAny<Book>())).Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Register(new Book() { ID = "ABC", Name = "Test Book" });

            store.Verify();
        }
コード例 #7
0
        public void Unblock_ProceedsWhenUserIsBlocked()
        {
            var user = this.userList.Where(u => u.Username == "edin").First();
            user.Status = UserStatus.Blocked;

            var store = new Mock<IBibliotecaStore>();
            store
                .Setup(x => x.GetUser(It.IsAny<string>())).Returns(user);
            store
                .Setup(x => x.UnblockUser(It.IsAny<User>()))
                .Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Unblock("edin");

            store.Verify();
        }
コード例 #8
0
        public void Return_ShouldBlockUserWhenExceedingTheReturnPeriod()
        {
            var returnDate = DateTime.Now.AddDays(40);
            var user = this.userList.Where(u => u.Username == "edin").First();
            user.AddLending(new LendingDetails(bookCatalog[0], user, DateTime.Now));

            var store = new Mock<IBibliotecaStore>();
            store
                .Setup(x => x.GetItem(It.Is<string>(i => i == "HEA1")))
                .Returns(this.bookCatalog
                    .Where(b => b.ID == "HEA1")
                    .FirstOrDefault());
            store
                .Setup(x => x.IsAvailableForLending(It.IsAny<LibraryItem>(), It.IsAny<DateTime>())).Returns(false);
            store
                .Setup(x => x.GetUser(It.IsAny<string>())).Returns(user);
            store
                .Setup(x => x.RemoveLending(It.IsAny<LibraryItem>(), It.IsAny<User>(), It.IsAny<DateTime>()))
                .Verifiable();
            store
                .Setup(x => x.BlockUser(It.IsAny<User>()))
                .Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            var userStatus = biblioteca.Return("HEA1", "edin", returnDate);

            store.Verify();
            Assert.AreEqual(UserStatus.Blocked, userStatus);
        }
コード例 #9
0
        public void Return_ShouldThrowExceptionWhenUserHasNoValidLending()
        {
            var user = this.userList.Where(u => u.Username == "edin").First();

            var store = new Mock<IBibliotecaStore>();
            store
                .Setup(x => x.GetItem(It.Is<string>(i => i == "HEA1")))
                .Returns(this.bookCatalog
                    .Where(b => b.ID == "HEA1")
                    .FirstOrDefault());
            store
                .Setup(x => x.IsAvailableForLending(It.IsAny<LibraryItem>(), It.IsAny<DateTime>())).Returns(false);
            store
                .Setup(x => x.GetUser(It.IsAny<string>())).Returns(user);

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Return("HEA1", "edin", DateTime.Now);   
        }
コード例 #10
0
        public void Return_ShouldThrowExceptionWhenUserIsNotValid()
        {
            var store = new Mock<IBibliotecaStore>();
            store.Setup(x => x.GetItem(It.Is<string>(i => i == "HEA1")))
                .Returns(this.bookCatalog
                    .Where(b => b.ID == "HEA1")
                    .FirstOrDefault());
            store.Setup(x => x.IsAvailableForLending(It.IsAny<LibraryItem>(), It.IsAny<DateTime>())).Returns(false);

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Return("HEA1", "inexistent user", DateTime.Now);   
        }
コード例 #11
0
        public void Lend_ShouldProceedWhenUserTriesToLendThirdBook()
        {
            var user = this.userList.Where(u => u.Username == "edin").First();
            user.AddLending(new LendingDetails(bookCatalog[0], user, DateTime.Now));
            user.AddLending(new LendingDetails(bookCatalog[1], user, DateTime.Now));

            var store = new Mock<IBibliotecaStore>();
            store.Setup(x => x.GetItem(It.IsAny<string>()))
                 .Returns((string id) => this.bookCatalog
                     .Where(b => b.ID == id)
                     .SingleOrDefault());
            store
                .Setup(x => x.GetUser(It.IsAny<string>())).Returns(user);
            store
                .Setup(x => x.IsAvailableForLending(It.IsAny<LibraryItem>(), It.IsAny<DateTime>())).Returns(true);
            store
                .Setup(x => x.AddLending(It.IsAny<LibraryItem>(), It.IsAny<User>(), It.IsAny<DateTime>()))
                .Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Lend("HEL1", "edin", DateTime.Now);

            store.Verify();
        }
コード例 #12
0
        public void Lend_ShouldProceedWithValidUserAndItem()
        {
            var store = new Mock<IBibliotecaStore>();
            store.Setup(x => x.GetItem(It.Is<string>(i => i == "HEA1")))
                 .Returns(this.bookCatalog
                     .Where(b => b.ID == "HEA1")
                     .FirstOrDefault());
            store
                .Setup(x => x.GetUser(It.IsAny<string>())).Returns(this.userList.Where(u => u.Username == "edin").First());
            store
                .Setup(x => x.IsAvailableForLending(It.IsAny<LibraryItem>(), It.IsAny<DateTime>())).Returns(true);
            store
                .Setup(x => x.AddLending(It.IsAny<LibraryItem>(), It.IsAny<User>(), It.IsAny<DateTime>()))
                .Verifiable();

            var biblioteca = new BibliotecaService(store.Object);
            biblioteca.Lend("HEA1", "edin", DateTime.Now);

            store.Verify();
        }