我有两个模型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)>'
我的测试中我做错了什么?有什么想法吗?
答案 0 :(得分:0)
我怀疑没有创建Privacy
对象。尝试拨打create!
而不是create
。如果它引发异常,那么这意味着您的Privacy
模型中有一些验证。
首先看过你的模型会非常有帮助。