I have two models Indicator belongs_to Privacy.
In indicator I wrote a method to return the proper name of the privacy setting
def privacy
Privacy.find(privacy_tag_id).content
end
That works in rails console. If I create an Indicator and then run Indicator.privacy I get back “red” or “green” or whatever. But I can’t get my rspec to pass. Here is the test
describe "indicator" do
it "should return a human named when asked for its privacy level" do
@privacy = PrivacyTag.create(:content => "Secret")
@ind = Indicator.new(:content => 'test',
:privacy_tag_id => @privacy.id)
@ind.privacy.should == "Secret"
end
end
When I run the test I get this message:
Failures:
1) indicator should return a human named when asked for its privacy level
Failure/Error: @ind.privacy.should_equal "Secret"
ActiveRecord::RecordNotFound:
Couldn't find PrivacyTag without an ID
# ./app/models/indicator.rb:13:in `privacy'
# ./spec/models/indicator_model_spec.rb:9:in `block (2 levels) in <top (required)>'
What am I doing wrong in my test? Any ideas?
I would suspect that the
Privacyobject isn’t being created. Try callingcreate!instead ofcreate. If it raises an exception, then that means that yourPrivacymodel has some validations in it.It would’ve been super helpful to have seen your models in the first place.