tab/spec/controllers/transactions_controller_spec.rb

95 lines
2.4 KiB
Ruby
Raw Normal View History

2015-09-12 11:03:04 +00:00
describe TransactionsController, type: :controller do
2015-09-09 12:37:40 +00:00
describe "creating transaction" do
before :each do
@debtor = create(:user)
@creditor = create(:user)
sign_in @debtor
end
2015-09-08 13:25:54 +00:00
2015-09-09 13:07:14 +00:00
context "with valid attributes" do
before :each do
2019-04-10 14:14:02 +00:00
@attributes = { params: { transaction: {
debtor: @debtor.name,
2015-09-09 12:37:40 +00:00
creditor: @creditor.name,
cents: 70,
2015-09-12 11:03:04 +00:00
message: "hoi"
2019-04-10 14:14:02 +00:00
} }}
2015-09-09 13:07:14 +00:00
end
it "should create a new transaction" do
2015-09-12 11:03:04 +00:00
expect { post :create, @attributes }.to change { Transaction.count }.by(1)
2015-09-09 13:07:14 +00:00
end
2015-09-11 14:11:56 +00:00
describe "fields" do
before :each do
post :create, @attributes
@transaction = Transaction.last
end
2015-09-09 13:07:14 +00:00
2015-09-11 14:11:56 +00:00
it "should set debtor" do
expect(@transaction.debtor).to eq(@debtor)
end
2015-09-09 13:07:14 +00:00
2015-09-11 14:11:56 +00:00
it "should set amount" do
expect(@transaction.amount).to eq(70)
end
it "should set creditor" do
expect(@transaction.creditor).to eq(@creditor)
end
2015-09-09 13:07:14 +00:00
2015-09-11 14:11:56 +00:00
it "should set issuer" do
expect(@transaction.issuer).to eq(@debtor)
end
2015-09-09 13:07:14 +00:00
end
2015-09-09 12:37:40 +00:00
end
context "with float euros" do
it "should set correct amount" do
2019-04-10 14:14:02 +00:00
post :create, params: { transaction: {
debtor: @debtor.name,
creditor: @creditor.name,
euros: 10.5,
message: "Omdat je een leuke jongen bent!"
2019-04-10 14:14:02 +00:00
}}
expect(Transaction.last.amount).to eq(1050)
end
end
context "with negative amount" do
it "should be refused" do
expect do
2019-04-10 14:14:02 +00:00
post :create, params: {transaction: attributes_for(:transaction, cents: -20)}
2015-09-12 11:03:04 +00:00
end.not_to change { Transaction.count }
end
end
2019-06-17 13:11:26 +00:00
context "with way to much money" do
it "should be refused" do
expect do
post :create, params:{ transaction: {
debtor: @debtor.name,
creditor: @creditor.name,
euros: 100000000000000,
message: "VEEL GELD"
}}
end.not_to change { Transaction.count }
end
end
context "for other user" do
it "should be refused" do
expect do
2019-04-10 14:14:02 +00:00
post :create, params:{ transaction: {
debtor: @creditor.name,
creditor: @debtor.name,
2019-06-17 13:11:26 +00:00
euros: 10,
2015-09-12 11:03:04 +00:00
message: "DIT IS OVERVAL"
2019-04-10 14:14:02 +00:00
}}
2015-09-12 11:03:04 +00:00
end.not_to change { Transaction.count }
end
end
2015-09-09 12:37:40 +00:00
end
2015-09-08 13:25:54 +00:00
end