patternrubyrailsMinor
RSpec tests for a cash flow model
Viewed 0 times
flowtestsforrspecmodelcash
Problem
This is my first model RSpec test:
```
require 'spec_helper'
describe CashFlow do
context 'DB Fields' do
it { should have_db_column :amount_cents }
it { should have_db_column :amount_currency }
it { should have_db_column :user_id }
it { should have_db_column :target_date }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
context 'Associations' do
it { should belong_to :user }
it { should have_and_belong_to_many :tags }
end
context 'Validation' do
it { should validate_presence_of :amount_cents }
it { should validate_numericality_of :amount_cents }
it { should ensure_exclusion_of(:amount_cents).in_array([0,]) }
it { should validate_presence_of :amount_currency }
it { should validate_presence_of :target_date }
it { should validate_presence_of :user }
end
context 'Scopes' do
subject { CashFlow }
context 'Incoming/Outcoming flows' do
let(:incoming_cash_flows) { 4.times.map { create :cash_flow } }
let(:outcoming_cash_flows) { 4.times.map { create :cash_flow, negative: true } }
it 'should be able to return all and only incomings cash_flows' do
expect(subject.incoming).to include *incoming_cash_flows
expect(subject.incoming).not_to include *outcoming_cash_flows
end
it 'should be able to return all and only outcomings cash_flows' do
expect(subject.outcoming).to include *outcoming_cash_flows
expect(subject.outcoming).not_to include *incoming_cash_flows
end
end
context 'Past/Future flows' do
[-1, nil, 1].each do |num|
_threshold = num ? Date.today + num.year : nil
let(:threshold) { _threshold }
let(:future_cash_flows) { 4.times.map { create :cash_flow, threshold: threshold } }
let(:past_cash_flows) { 4.times.map { create :cash_flow, past: true, threshold: threshold } }
it "should be able to return all and only past cash_flows with
```
require 'spec_helper'
describe CashFlow do
context 'DB Fields' do
it { should have_db_column :amount_cents }
it { should have_db_column :amount_currency }
it { should have_db_column :user_id }
it { should have_db_column :target_date }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
end
context 'Associations' do
it { should belong_to :user }
it { should have_and_belong_to_many :tags }
end
context 'Validation' do
it { should validate_presence_of :amount_cents }
it { should validate_numericality_of :amount_cents }
it { should ensure_exclusion_of(:amount_cents).in_array([0,]) }
it { should validate_presence_of :amount_currency }
it { should validate_presence_of :target_date }
it { should validate_presence_of :user }
end
context 'Scopes' do
subject { CashFlow }
context 'Incoming/Outcoming flows' do
let(:incoming_cash_flows) { 4.times.map { create :cash_flow } }
let(:outcoming_cash_flows) { 4.times.map { create :cash_flow, negative: true } }
it 'should be able to return all and only incomings cash_flows' do
expect(subject.incoming).to include *incoming_cash_flows
expect(subject.incoming).not_to include *outcoming_cash_flows
end
it 'should be able to return all and only outcomings cash_flows' do
expect(subject.outcoming).to include *outcoming_cash_flows
expect(subject.outcoming).not_to include *incoming_cash_flows
end
end
context 'Past/Future flows' do
[-1, nil, 1].each do |num|
_threshold = num ? Date.today + num.year : nil
let(:threshold) { _threshold }
let(:future_cash_flows) { 4.times.map { create :cash_flow, threshold: threshold } }
let(:past_cash_flows) { 4.times.map { create :cash_flow, past: true, threshold: threshold } }
it "should be able to return all and only past cash_flows with
Solution
First thing: congratulations on starting with rspec! Way to go.
My first remark would be to remove your three first contexts (Fields, Associations & Validations).
Why? Because those are not testing any parts of your code - they are testing ActiveModel functionalities. You want to test your code, not your librairies - at least, not without a very good reason to think that there is a problem there. This is time spent to write specs that do not provide any benefits.
On the other side, while your model don't have much logic yet, I like the fact that you tested your scopes - this is your logic, and it should be tested.
My first remark would be to remove your three first contexts (Fields, Associations & Validations).
Why? Because those are not testing any parts of your code - they are testing ActiveModel functionalities. You want to test your code, not your librairies - at least, not without a very good reason to think that there is a problem there. This is time spent to write specs that do not provide any benefits.
On the other side, while your model don't have much logic yet, I like the fact that you tested your scopes - this is your logic, and it should be tested.
Context
StackExchange Code Review Q#44743, answer score: 2
Revisions (0)
No revisions yet.