public ActionResult Create(Models.TransferTransaction.CreateModel model)
        {
            var transferTransaction = new TransferTransaction();
            transferTransaction.Configure(model.Amount)
                .ForDate(model.Date)
                .WithComments(model.Comments)
                .FromAccount(_repository.Find<Account>(model.SelectedFromAccountId))
                .ToAccount(_repository.Find<Account>(model.SelectedToAccountId));

            _repository.Save(transferTransaction);

            return RedirectToAction("Index", "Transaction");
        }
        public void Configure_ShouldSetPropertiesAsExpected()
        {
            const decimal amount = 100;
            var date = DateTime.Today.AddDays(1);
            const string comments = "something";
            var assetAccount = new AssetAccount();
            var liabilityAccount = new LiabilityAccount();

            var transaction = new TransferTransaction();
            transaction.Configure(amount)
                    .ForDate(date)
                    .WithComments(comments)
                    .FromAccount(assetAccount)
                    .ToAccount(liabilityAccount);

            Assert.That(transaction.Date, Is.EqualTo(date));
            Assert.That(transaction.Comments, Is.EqualTo(comments));
            Assert.That(transaction.Entries.Count, Is.EqualTo(2));
            Assert.That(transaction.Entries.Any(x => (x.Amount == amount) && (x.Type == EntryType.Credit) && (x.Account == assetAccount)), Is.True);
            Assert.That(transaction.Entries.Any(x => (x.Amount == amount) && (x.Type == EntryType.Debit) && (x.Account == liabilityAccount)), Is.True);
        }
        public void Edit_ShouldSaveTransactionToRepository()
        {
            const decimal amount = 100;
            var date = DateTime.Today.AddDays(1);
            const string comments = "something";
            var assetAccount1 = new AssetAccount();
            var assetAccount2 = new AssetAccount();
            var TransferTransaction = new TransferTransaction();

            var model = new EditModel();
            model.Id = TransferTransaction.Id;
            model.Amount = amount;
            model.Date = date;
            model.Comments = comments;
            model.SelectedFromAccountId = assetAccount1.Id;
            model.SelectedToAccountId = assetAccount2.Id;

            _repository.Stub(x => x.Find<TransferTransaction>(TransferTransaction.Id)).Return(TransferTransaction);
            _repository.Stub(x => x.Find<Account>(assetAccount1.Id)).Return(assetAccount1);
            _repository.Stub(x => x.Find<Account>(assetAccount2.Id)).Return(assetAccount2);
            _repository.Expect(x => x.Save(Arg<TransferTransaction>.Matches(
                y =>
                    (y.Date == date) &&
                    (y.Comments == comments) &&
                    (y.Entries.Count == 2) &&
                    (y.Entries.Any(z => (z.Account.Id == assetAccount1.Id) && (z.Amount == amount) && (z.Type == EntryType.Credit))) &&
                    (y.Entries.Any(z => (z.Account.Id == assetAccount2.Id) && (z.Amount == amount) && (z.Type == EntryType.Debit)))))).Return(null);

            var controller = GetController();
            controller.Edit(model);

            _repository.VerifyAllExpectations();
        }
        public void Edit_ShouldReturnViewWithModel()
        {
            var assetAccount1 = new AssetAccount();
            var assetAccount2 = new AssetAccount();
            var liabilityAccount1 = new LiabilityAccount();
            var liabilityAccount2 = new LiabilityAccount();
            var accounts = new Account[] { assetAccount1, assetAccount2, liabilityAccount1, liabilityAccount2 };

            const string comments = "something";
            const decimal amount = 100;
            var date = DateTime.Today.AddDays(1);

            var TransferTransaction = new TransferTransaction();
            TransferTransaction.Configure(amount)
                .ForDate(date)
                .WithComments(comments)
                .FromAccount(assetAccount1)
                .ToAccount(assetAccount2);

            _repository.Stub(x => x.Find<TransferTransaction>(TransferTransaction.Id)).Return(TransferTransaction);
            _repository.Stub(x => x.Get<Account>()).Return(accounts);

            var controller = GetController();
            var result = (ViewResult)controller.Edit(TransferTransaction.Id);

            Assert.That(result.ViewName, Is.EqualTo("Edit"));

            var viewModel = (EditModel)result.Model;
            Assert.That(viewModel.Id, Is.EqualTo(TransferTransaction.Id));
            Assert.That(viewModel.AssetAndLiabilityAccounts.Count(), Is.EqualTo(4));
            Assert.That(viewModel.AssetAndLiabilityAccounts.Any(x => x.Id == assetAccount1.Id), Is.True);
            Assert.That(viewModel.AssetAndLiabilityAccounts.Any(x => x.Id == assetAccount2.Id), Is.True);
            Assert.That(viewModel.AssetAndLiabilityAccounts.Any(x => x.Id == liabilityAccount1.Id), Is.True);
            Assert.That(viewModel.AssetAndLiabilityAccounts.Any(x => x.Id == liabilityAccount2.Id), Is.True);
            Assert.That(viewModel.SelectedFromAccountId, Is.EqualTo(assetAccount1.Id));
            Assert.That(viewModel.SelectedToAccountId, Is.EqualTo(assetAccount2.Id));
            Assert.That(viewModel.Date, Is.EqualTo(date));
            Assert.That(viewModel.Amount, Is.EqualTo(amount));
            Assert.That(viewModel.Comments, Is.EqualTo(comments));
        }
        public void Edit_ShouldReturnRedirectToIndex()
        {
            var TransferTransaction = new TransferTransaction();
            var model = new EditModel();
            model.Id = TransferTransaction.Id;
            model.SelectedFromAccountId = Guid.NewGuid();
            model.SelectedToAccountId = Guid.NewGuid();

            _repository.Stub(x => x.Find<TransferTransaction>(TransferTransaction.Id)).Return(TransferTransaction);
            _repository.Stub(x => x.Find<Account>(model.SelectedFromAccountId)).Return(new AssetAccount());
            _repository.Stub(x => x.Find<Account>(model.SelectedToAccountId)).Return(new AssetAccount());

            var controller = GetController();
            var result = (RedirectToRouteResult)controller.Edit(model);

            Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
            Assert.That(result.RouteValues["controller"], Is.EqualTo("Transaction"));
        }