Skip to content

Commit

Permalink
Simplify Plutus::Transaction spec
Browse files Browse the repository at this point in the history
  • Loading branch information
willglynn committed Feb 18, 2013
1 parent b112ca1 commit b7db6b6
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions spec/models/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@

module Plutus
describe Transaction do
let(:transaction) { FactoryGirl.build(:transaction) }
subject { transaction }

it "should create a transaction" do
transaction = FactoryGirl.build(:transaction_with_credit_and_debit)
transaction.save!
end
it { should_not be_valid }

it "should not be valid without a credit amount" do
transaction = FactoryGirl.build(:transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :transaction => transaction)
transaction.should_not be_valid
transaction.errors['base'].should include("Transaction must have at least one credit amount")
context "with credit and debit" do
let(:transaction) { FactoryGirl.build(:transaction_with_credit_and_debit) }
it { should be_valid }

it "should require a description" do
transaction.description = nil
transaction.should_not be_valid
end
end

it "should not be valid with an invalid credit amount" do
transaction = FactoryGirl.build(:transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :transaction => transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :transaction => transaction, :amount => nil)
transaction.should_not be_valid
transaction.errors[:credit_amounts].should == ["is invalid"]
end
context "with a debit" do
before {
transaction.debit_amounts << FactoryGirl.build(:debit_amount, transaction: transaction)
}
it { should_not be_valid }

it "should not be valid without a debit amount" do
transaction = FactoryGirl.build(:transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :transaction => transaction)
transaction.should_not be_valid
transaction.errors['base'].should include("Transaction must have at least one debit amount")
context "with an invalid credit" do
before {
transaction.credit_amounts << FactoryGirl.build(:credit_amount, transaction: transaction, amount: nil)
}
it { should_not be_valid }
end
end

it "should not be valid with an invalid debit amount" do
transaction = FactoryGirl.build(:transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :transaction => transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :transaction => transaction, :amount => nil)
transaction.should_not be_valid
transaction.errors[:debit_amounts].should == ["is invalid"]
end
context "with a credit" do
before {
transaction.credit_amounts << FactoryGirl.build(:credit_amount, transaction: transaction)
}
it { should_not be_valid }

it "should not be valid without a description" do
transaction = FactoryGirl.build(:transaction_with_credit_and_debit, :description => nil)
transaction.should_not be_valid
transaction.errors[:description].should == ["can't be blank"]
context "with an invalid debit" do
before {
transaction.debit_amounts << FactoryGirl.build(:debit_amount, transaction: transaction, amount: nil)
}
it { should_not be_valid }
end
end

it "should require the debit and credit amounts to cancel" do
transaction = FactoryGirl.build(:transaction)
transaction.credit_amounts << FactoryGirl.build(:credit_amount, :amount => 100, :transaction => transaction)
transaction.debit_amounts << FactoryGirl.build(:debit_amount, :amount => 200, :transaction => transaction)
transaction.should_not be_valid
Expand All @@ -54,16 +54,16 @@ module Plutus

it "should have a polymorphic commercial document associations" do
mock_document = FactoryGirl.create(:asset) # one would never do this, but it allows us to not require a migration for the test
transaction = FactoryGirl.build(:transaction_with_credit_and_debit, :commercial_document => mock_document)
transaction.save
transaction = FactoryGirl.build(:transaction_with_credit_and_debit, commercial_document: mock_document)
transaction.save!
saved_transaction = Transaction.find(transaction.id)
saved_transaction.commercial_document.should == mock_document
end

it "should allow building a transaction and credit and debits with a hash" do
FactoryGirl.create(:asset, :name => "Accounts Receivable")
FactoryGirl.create(:revenue, :name => "Sales Revenue")
FactoryGirl.create(:liability, :name => "Sales Tax Payable")
FactoryGirl.create(:asset, name: "Accounts Receivable")
FactoryGirl.create(:revenue, name: "Sales Revenue")
FactoryGirl.create(:liability, name: "Sales Tax Payable")
mock_document = FactoryGirl.create(:asset)
transaction = Transaction.build(
description: "Sold some widgets",
Expand All @@ -73,8 +73,8 @@ module Plutus
credits: [
{account: "Sales Revenue", amount: 45},
{account: "Sales Tax Payable", amount: 5}])
transaction.should be_valid
transaction.save
transaction.save!

saved_transaction = Transaction.find(transaction.id)
saved_transaction.commercial_document.should == mock_document
end
Expand Down

0 comments on commit b7db6b6

Please sign in to comment.