2013-10-02 21 views
1

模型早該-的匹配存在真正的節省

class RecipeIngredient < ActiveRecord::Base 
    validates :recipe_id, presence: true, on: :save 
    validates :ingredient_id, presence: true, on: :save 

    belongs_to :recipe 
    belongs_to :ingredient 

    has_many :quantities 
end 

測試

require 'spec_helper' 

describe RecipeIngredient do 
    it { should validate_presence_of(:recipe_id) } 
    it { should validate_presence_of(:ingredient_id) } 
    it { should belong_to(:recipe) } 
    it { should belong_to(:ingredient) } 

    context "valid" do 
     subject { RecipeIngredient.new(recipe_id: 1, ingredient_id: 1) } 
     it { should be_valid } 
    end 
end 

回報

Failures:

1) RecipeIngredient Failure/Error: it { should validate_presence_of(:recipe_id) } Did not expect errors to include "can't be blank" when recipe_id is set to nil, got error: # ./spec/models/recipe_ingredient_spec.rb:4:in `block (2 levels) in '

2) RecipeIngredient Failure/Error: it { should validate_presence_of(:ingredient_id) } Did not expect errors to include "can't be blank" when ingredient_id is set to nil, got error: # ./spec/models/recipe_ingredient_spec.rb:5:in `block (2 levels) in '

我不明白爲什麼上添加:保存打破這個測試

回答

-1

首先,你不需要把on: :save條款。這是默認設置,可以關閉。

+0

這是不正確的!我的意思是如果你不穿戴::當你運行object.valid時,保存你的validatons會被運行嗎?如果你把它,他們不會跑!所以我仍然認爲這應該是一個錯誤! –

相關問題