我有两个模型 Indicator belongs_to Privacy。
在指标中,我编写了一个方法来返回隐私设置的正确名称
def privacy
Privacy.find(privacy_tag_id).content
end
这适用于rails控制台。如果我创建一个指标然后运行 Indicator.privacy 我会返回“红色”或“绿色”或其他任何内容。但我无法让我的 rspec 通过。这是测试
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
当我运行测试时,我收到以下消息:
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)>'
我在测试中做错了什么?有任何想法吗?