public void Borrower_ShouldPersistAndRecall()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder()
                                .WithRandomProps()
                                .WithProp(b => b.Id = 0)
                                .WithValidTitleId()
                                .Build();
            //---------------Assert Precondition----------------

            using (var ctx = new LendingLibraryDbContext(_localDb.CreateConnection()))
            {
                //ctx.Set<Borrower>().Add(borrower);
                //ctx.Set(typeof (Borrower)).Add(borrower);
                //ctx.Set(borrower.GetType()).Add(borrower);
                //ctx.Entry(borrower).State = EntityState.Added;

                ctx.Borrowers.Add(borrower);
                ctx.SaveChanges();
            }

            using (var ctx = new LendingLibraryDbContext(_localDb.CreateConnection()))
            {
                //---------------Execute Test ----------------------
                var result = ctx.Set<Borrower>().Single();
                //---------------Test Result -----------------------
                PropertyAssert.AllPropertiesAreEqual(borrower, result, "Title");
            }

          
        }
        public void Get_GivenBorrowerDoesNotExistForId_ShouldReturnBorrower()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder()
                .WithRandomProps().Build();
            var dbContext = new TestDbContextBuilder()
                .WithBorrowers(borrower)
                .Build();

            var repository = new BorrowerRepository(dbContext);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var actualBorrower = repository.Get(borrower.Id + 1);
            //---------------Test Result -----------------------
            Assert.IsNull(actualBorrower);
        }
        public void GetAll_GivenOneBorrower_ShouldReturnBorrower()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder()
                .WithRandomProps().Build();

            var dbContext = new TestDbContextBuilder()
                .WithBorrowers(borrower)
                .Build();

            var repository = new BorrowerRepository(dbContext);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var borrowers = repository.GetAll();
            //---------------Test Result -----------------------
            Assert.AreEqual(1, borrowers.Count);
            var actual = borrowers.First();
            Assert.AreSame(borrower, actual);
        }
        public void EditGET_GivenValidBorrowerItemId_ShouldReturnBorrowerViewModel()
        {
            //---------------Set up test pack-------------------
            var borrowerItems= new BorrowerItemBuilder().WithRandomProps().Build();
            var id = borrowerItems.Id;

            var repository = Substitute.For<IBorrowerItemRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var mapper = Substitute.For<IMappingEngine>();

            repository.Get(id).Returns(borrowerItems);

            var item = new Item { Id = RandomValueGen.GetRandomInt(), Description = RandomValueGen.GetRandomString() };
            var items = new List<Item> { item };
            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var borrowers = new List<Borrower> { borrower };


            itemRepository.GetAll().Returns(items);
            borrowerRepository.GetAll().Returns(borrowers);
          

            
            var borrowerItemViewModel = new BorrowerItemViewModel { Id = RandomValueGen.GetRandomInt() };
            
            mapper.Map<BorrowerItemViewModel>(borrowerItems).Returns(borrowerItemViewModel);

            var borrowerItemController = CreateBuilder()
                .WithBorrowerItemRepository(repository)
                .WithBorrowerRepository(borrowerRepository)
                .WithItemRepository(itemRepository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = borrowerItemController.Edit(id) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            var model = result.Model as BorrowerItemViewModel;
            Assert.IsNotNull(model);
            Assert.AreEqual(1, model.BorrowersSelectListItems.Count);
            Assert.AreEqual(1, model.ItemSelectListItems.Count);
        }
        public void Create_POST_ShouldCallSaveAndRedirectToIndex()
        {
            //---------------Set up test pack-------------------
            var borrowerItemRepository = Substitute.For<IBorrowerItemRepository>();
            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var itemRepository = Substitute.For<IItemRepository>();
            var mapper = Substitute.For<IMappingEngine>();

            var borrowersItem = new BorrowerItemBuilder().WithRandomProps().Build();
            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var item = new ItemBuilder().WithRandomProps().Build();

            var borrowerItemViewModel = new BorrowerItemViewModel();

            mapper.Map<BorrowersItem>(borrowerItemViewModel).Returns(borrowersItem);

            itemRepository.Get(borrowerItemViewModel.ItemId).Returns(item);

            borrowersItem.ItemId = item.Id;
            borrowersItem.BorrowerId = borrower.Id;
            borrowerRepository.Get(borrowerItemViewModel.BorrowerId).Returns(borrower);

            borrowerItemViewModel.ItemId = item.Id;
            borrowerItemViewModel.BorrowerId = borrower.Id;
            borrowerItemViewModel.DateBorrowed=DateTime.Now;
            borrowerItemViewModel.DateReturned=DateTime.Now.ToString();

           
            
            var borrowerItemController = CreateBuilder()
                   .WithBorrowerItemRepository(borrowerItemRepository)
                   .WithBorrowerRepository(borrowerRepository)
                   .WithItemRepository(itemRepository)
                   .WithMappingEngine(mapper)
                   .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerItemController.Create(borrowerItemViewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            borrowerItemRepository.Received().Save(borrowersItem);
            Assert.IsNotNull(result);
            var actionName = result.RouteValues["action"];
            Assert.AreEqual("Index", actionName);
        }
        public void Delete_GivenBorrowerItemExists_ShouldDeleteBorrowerAndCallSavedChanges()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder()
                   .WithRandomProps().Build();

            var dbContext = new TestDbContextBuilder()
                .WithBorrowers(borrower)
                .Build();

            var repository = CreateBuilder().WithDbContext(dbContext).Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            repository.Delete(borrower);
            //---------------Test Result -----------------------
            Assert.AreEqual(0, dbContext.Borrowers.Count());
            dbContext.Received().SaveChanges();
        } 
        public void Save_GivenNewBorrower_ShouldSave()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder()
                   .WithRandomProps()
                   .WithNewId()
                   .Build();

            var dbContext = new TestDbContextBuilder().Build();

            var repository = CreateBuilder().WithDbContext(dbContext).Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            repository.Save(borrower);
            //---------------Test Result -----------------------
            dbContext.Received().AttachEntity(borrower);
            dbContext.Received().SaveChanges();
        }
        public void Details_GivenValidUserId_ShouldReturnBorrowersDetails()
        {
            //---------------Set up test pack-------------------

            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var id = borrower.Id;

            var repository = Substitute.For<IBorrowerRepository>();
            repository.Get(id).Returns(borrower);

            var mapper = Substitute.For<IMappingEngine>();
            var borrowerViewModel = new BorrowerViewModel { Id = RandomValueGen.GetRandomInt() };
            mapper.Map<BorrowerViewModel>(borrower).Returns(borrowerViewModel);

            var borrowerController = CreateBuilder()
                .WithBorrowerRepository(repository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerController.Details(id) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            var model = result.Model as BorrowerViewModel;
            Assert.IsNotNull(model);
        }
        public void Edit_GivenValidBorrowerId_ShouldReturnBorrowerViewModelWithTitlesSelected()
        {
            //---------------Set up test pack-------------------
            var borrower = new BorrowerBuilder().WithRandomProps().Build();
            var id = borrower.Id;

            var repository = Substitute.For<IBorrowerRepository>();
            repository.Get(id).Returns(borrower);

            var title1 = TitleBuilder.BuildRandom();
            var title2 = TitleBuilder.BuildRandom();
            var title3 = TitleBuilder.BuildRandom();
            var titles = new List<Title> { title1, title2, title3 };

            repository.GetAllTitles().Returns(titles);

            var mapper = Substitute.For<IMappingEngine>();

            var borrowerViewModel = new BorrowerViewModel { Id = RandomValueGen.GetRandomInt() ,TitleId = title2.Id};         
            mapper.Map<BorrowerViewModel>(borrower).Returns(borrowerViewModel);

            var borrowerController = CreateBuilder()
                .WithBorrowerRepository(repository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = borrowerController.Edit(id) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            var model = result.Model as BorrowerViewModel;
            Assert.IsNotNull(model);
            Assert.AreEqual(3,model.TitlesSelectList.Count);

            var selectListItem2 = model.TitlesSelectList[1];
            Assert.AreEqual(title2.Id.ToString(), selectListItem2.Value);
            Assert.AreEqual(title2.Description, selectListItem2.Text);
            Assert.IsTrue(selectListItem2.Selected);

        }
        public void Create_POST_ShouldCallSaveAndRedirectToIndex()
        {
            var borrower = new BorrowerBuilder()
                .WithRandomProps()
                .Build();
           
            var file = Substitute.For<HttpPostedFileBase>();
            file.FileName.Returns("somefileName");
            file.ContentLength.Returns(Int32.MaxValue);
            file.ContentType.Returns(String.Empty);
            file.InputStream.Returns(Stream.Null);
            
            var repository = Substitute.For<IBorrowerRepository>();
            var title = new TitleBuilder().WithRandomProps().Build();
            borrower.TitleId = title.Id;
            repository.GetTitleById(borrower.TitleId).Returns(title);

            var mapper = Substitute.For<IMappingEngine>();
            var borrowerViewModel = new BorrowerViewModel ();

            mapper.Map<Borrower>(borrowerViewModel).Returns(borrower);
            borrowerViewModel.Id = borrower.Id;
            borrowerViewModel.ContactNumber = borrower.ContactNumber;
            borrowerViewModel.Email = borrower.Email;
            borrowerViewModel.FirstName = borrower.FirstName;
            borrowerViewModel.Surname = borrower.Surname;
            borrowerViewModel.Photo = borrower.Photo;
            borrowerViewModel.TitleId = borrower.TitleId;

            var borrowerController = CreateBuilder()
                .WithBorrowerRepository(repository)
                .WithMappingEngine(mapper)
                .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerController.Create(borrowerViewModel, file) as RedirectToRouteResult;

            //---------------Test Result -----------------------
            Assert.AreSame(title, borrower.Title);
            repository.Received().Save(borrower);

            Assert.IsNotNull(result);
            var actionName = result.RouteValues["action"];
            Assert.AreEqual("Index", actionName);
        }