Reputation:
I am totally new to Rails and testing and I wrote this model:
class KeyPerformanceInd < ActiveRecord::Base
#attr_accessible :name, :organization_id, :target
include ActiveModel::ForbiddenAttributesProtection
belongs_to :organization
has_many :key_performance_intervals, :foreign_key => 'kpi_id'
validates :name, presence: true
validates :target, presence: true
validates :organization_id, presence: true
end
My question is for a model like that, what are some RSpec tests that I should write? Somethings like this? Or there are moe things to do? I hear about FactoryGirl, Is that something I need for testing this model or that is for testing stuff in the controller?
Describe KeyPerformanceInd do
it {should belong_to(:key_performance_interval)}
end
Upvotes: 2
Views: 148
Reputation: 2964
In this case, you don't need to do more, and you can also use the shoulda-matchers gem to make your code really clean :
it { should belong_to(:organization) }
it { should have_many(:key_performance_intervals) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:target) }
it { should validate_presence_of(:organization_id) }
And this is it.
You don't need FactoryGirl
in this case, which is used to create valid and re-usable objects. But you could use factories in your model test. A simple example :
Your factory :
FactoryGirl.define do
factory :user do
first_name "John"
last_name "Doe"
end
end
Your test :
it "should be valid with valid attributes" do
user = FactoryGirl.create(:user)
user.should be_valid
end
Check the Factory Girl documentation to have more informations.
Upvotes: 7