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
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 :
And this is it.
You don’t need
FactoryGirlin 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 :
Your test :
Check the Factory Girl documentation to have more informations.