[英]rspec model testing in rails: method works but test wont pass
I have two models Indicator belongs_to Privacy. 我有两个模型指标:belongs_to隐私。
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. 在rails控制台中工作。 If I create an Indicator and then run Indicator.privacy I get back "red" or "green" or whatever.
如果我创建一个指标,然后运行Indicator.privacy,我将返回“红色”或“绿色”或其他内容。 But I can't get my rspec to pass.
但是我无法通过我的rspec。 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 Privacy
object isn't being created. 我怀疑没有创建
Privacy
对象。 Try calling create!
尝试致电
create!
instead of create
. 而不是
create
。 If it raises an exception, then that means that your Privacy
model has some validations in it. 如果引发异常,则意味着您的
Privacy
模型中包含一些验证。
It would've been super helpful to have seen your models in the first place. 首先看到您的模型将对您很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.