Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中的rspec模型测试:方法可行,但测试无法通过_Ruby On Rails_Testing_Rspec - Fatal编程技术网

Ruby on rails rails中的rspec模型测试:方法可行,但测试无法通过

Ruby on rails rails中的rspec模型测试:方法可行,但测试无法通过,ruby-on-rails,testing,rspec,Ruby On Rails,Testing,Rspec,我有两个属于隐私的型号 在indicator中,我编写了一个方法来返回隐私设置的正确名称 def privacy Privacy.find(privacy_tag_id).content end 在rails控制台中工作。如果我创建了一个指示器,然后运行Indicator.privacy,我会得到“红色”或“绿色”或其他信息。但我无法通过我的rspec。这是测试 describe "indicator" do it "should return a human named wh

我有两个属于隐私的型号

在indicator中,我编写了一个方法来返回隐私设置的正确名称

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)>'
故障:
1) 当询问指示器的隐私级别时,指示器应返回一个名为的人
失败/错误:@ind.privacy.should_等于“Secret”
ActiveRecord::RecordNotFound:
找不到没有ID的PrivacyTag
#./app/models/indicator.rb:13:in“隐私”
#./spec/models/indicator\u model\u spec.rb:9:in'block(2层)in'

我在考试中做错了什么?有什么想法吗?

我怀疑没有创建
隐私
对象。尝试调用
create而不是
创建
。如果它引发了一个异常,那么这意味着您的
隐私
模型中有一些验证


如果你一开始就看到了你的模型,那会非常有帮助。

这是一次很好的验证。这正是问题所在。很抱歉没有包括模型。