I'm newbie on rails and I have to write tests for existing rails apps with 'Rspec','shoulda' and 'factory girl' gems. I can test non specific tests like validates_presence_of: something with 'sholda' matchers. But I want to test methods which in models. I can visualize what I need to do, but I can't compose.
This is an example what I'm talking about:
.
.
.
context 'is editable if project is not started' do
setup do
@brief=Factory(:brief)
@started_project=Factory(:project_started, :brief => @brief)
@brief_have_no_project=Factory(:brief)
end
specify "with editable brief" do
@brief.brand_info = 'bla bla bla' #change brand info, this is impossible
#i can't compose this section :S
end
specify "with non-editable brief" do
end
end
.
.
.
I want to test is brief editable in this code. How can I test it?
This is the brief models code:
class Brief < ActiveRecord::Base
belongs_to :project
validate :can_only_be_edited_if_project_is_not_started
.
.
.
def can_only_be_edited_if_project_is_not_started
errors.add(:project_id, 'can_only_be_edited_if_project_is_not_started') if
!project.nil? && !project.brief_can_be_edited?
end
.
.
.
end
I will be very happy if I can find a starting point. Thanks for help. :)
Failures:
1) Brief is editable if project is not started with editable brief.
Failure/Error: @brief.brand_info = 'bla bla bla'
NoMethodError:
undefined method `brand_info=' for nil:NilClass
# ./spec/models/brief_spec.rb:34:in `block (3 levels) in <top (required)>'
when try to assign value like this @brief.brand_info = 'bla bla bla'