tab/spec/controllers/transactions_controller_spec.rb

79 lines
2 KiB
Ruby
Raw Normal View History

require 'rails_helper'
require 'spec_helper'
RSpec.describe TransactionsController, type: :controller do
2015-09-09 14:37:40 +02:00
describe "creating transaction" do
before :each do
@debtor = create(:user)
@creditor = create(:user)
sign_in @debtor
end
2015-09-08 15:25:54 +02:00
2015-09-09 15:07:14 +02:00
context "with valid attributes" do
before :each do
@attributes = { transaction: {
debtor: @debtor.name,
2015-09-09 14:37:40 +02:00
creditor: @creditor.name,
cents: 70,
2015-09-09 15:07:14 +02:00
message: 'hoi'
2015-09-09 14:37:40 +02:00
}}
2015-09-09 15:07:14 +02:00
post :create, @attributes
@transaction = Transaction.last
end
it "should create a new transaction" do
expect {post :create, @attributes}.to change {Transaction.count}.by(1)
2015-09-09 15:07:14 +02:00
end
it "should set debtor" do
expect(@transaction.debtor).to eq(@debtor)
end
it "should set amount" do
expect(@transaction.amount).to eq(70)
2015-09-09 15:07:14 +02:00
end
it "should set creditor" do
expect(@transaction.creditor).to eq(@creditor)
end
it "should set issuer" do
expect(@transaction.issuer).to eq(@debtor)
end
2015-09-09 14:37:40 +02:00
end
context "with float euros" do
it "should set correct amount" do
post :create, transaction: {
debtor: @debtor.name,
creditor: @creditor.name,
euros: 10.5,
message: "Omdat je een leuke jongen bent!"
}
expect(Transaction.last.amount).to eq(1050)
end
end
context "with negative amount" do
it "should be refused" do
expect do
post :create, transaction: attributes_for(:transaction, cents: -20)
end.not_to change {Transaction.count}
end
end
context "for other user" do
it "should be refused" do
expect do
post :create, transaction: {
debtor: @creditor.name,
creditor: @debtor.name,
euros: 10000000,
message: 'DIT IS OVERVAL'
}
end.not_to change {Transaction.count}
end
end
2015-09-09 14:37:40 +02:00
end
2015-09-08 15:25:54 +02:00
end