1

私は 2 つのモデルを持っています。

インジケーターでは、プライバシー設定の適切な名前を返すメソッドを書きました

def privacy
    Privacy.find(privacy_tag_id).content
end

それはレールコンソールで動作します。Indicator を作成して 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)>'

テストで何が間違っていますか? 何か案は?

4

1 に答える 1

0

Privacyオブジェクトが作成されていないと思われます。create!の代わりに電話してみてくださいcreate。例外が発生した場合、それはPrivacyモデルにいくつかの検証があることを意味します。

最初にあなたのモデルを見たことが非常に役に立ちました。

于 2012-08-31T19:45:19.750 に答える