First off, you don't need to put the on: :save
clause. That is the default and can be left off.
shoulda-matchers presence true on save
-
30-06-2022 - |
Question
model
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
test
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
returns
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 '
I don't understand why adding on: :save has broken this test
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow