public ActionResult Create(BorrowerViewModel viewModel, HttpPostedFileBase file = null)
        {
            if(file==null)
                ModelState.AddModelError("ImageNameCannotBeNull", "Please select an image");
            if (viewModel.FirstName==null)
                ModelState.AddModelError("ImageNameCannotBeNull", "Please enter First Name");
            if (viewModel.Surname == null)
                ModelState.AddModelError("ImageNameCannotBeNull", "Please enter Surname");
            if (viewModel.Email == null)
                ModelState.AddModelError("ImageNameCannotBeNull", "Please enter email");
        
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    var photo = ReadFully(file.InputStream);
                    viewModel.Photo = photo;
                }

                var borrower = _mappingEngine.Map<Borrower>(viewModel);
                var title = _borrowerRepository.GetTitleById(viewModel.TitleId);
                borrower.Title = title;
                if (file != null) borrower.ContentType = file.ContentType.ToLower();
                _borrowerRepository.Save(borrower);
                return RedirectToAction("Index");
            }
            viewModel.TitlesSelectList = GetTitles(viewModel.TitleId);
            return View(viewModel);

        }
    // GET: BorrowerViewModel/Create
 
    public ActionResult Create()
    {
        var viewModel = new BorrowerViewModel();
        viewModel.TitlesSelectList = GetTitles(null);
        return View(viewModel);
    }
        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 ActionResult Delete(BorrowerViewModel viewModel)
        {
            try
            {
                var borrower = _mappingEngine.Map<Borrower>(viewModel);
                _borrowerRepository.Delete(borrower);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public void Delete_GivenExceptionWhenRepositoryDeleteIsCalled_ShouldReturnView()
        {
            //---------------Set up test pack-------------------
            var borrowerViewModel = new BorrowerViewModel();

            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            borrowerRepository.When(repository => repository.Delete(Arg.Any<Borrower>()))
                .Throw<ApplicationException>();

            var borrowerController = CreateBuilder()
                .WithBorrowerRepository(borrowerRepository)
                .Build();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            var result = borrowerController.Delete(borrowerViewModel) as ViewResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
       }
        public void Delete_GivenBorrowerViewModel_ShouldRedirectToIndexAfterDelete()
        {
            //---------------Set up test pack-------------------
            var borrowerViewModel = new BorrowerViewModel();
            var borrowerController = CreateBuilder()
               .Build();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = borrowerController.Delete(borrowerViewModel) as RedirectToRouteResult;
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            Assert.AreEqual("Index",result.RouteValues["action"]);
        }
        public void Delete_GivenBorrowerViewModel_ShouldMapToBorrowerAndDelete()
        {
            //---------------Set up test pack-------------------
            var borrowerViewModel = new BorrowerViewModel();
            var borrower = BorrowerBuilder.BuildRandom();
            var mappingEngine = Substitute.For<IMappingEngine>();
            mappingEngine.Map<Borrower>(borrowerViewModel).Returns(borrower);

            var borrowerRepository = Substitute.For<IBorrowerRepository>();
            var borrowerController = CreateBuilder()
                .WithMappingEngine(mappingEngine)
                .WithBorrowerRepository(borrowerRepository)
                .Build();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var result = borrowerController.Delete(borrowerViewModel);
            //---------------Test Result -----------------------
            Assert.IsNotNull(result);
            borrowerRepository.Received(1).Delete(borrower);
            
        }
        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);
        }