Shoulda: Test validates_presence_of: on =>: update

I use Shoulda in conjunction with Test :: Unit in one of the projects I'm working on. The problem I am facing is that I recently changed this:

class MyModel < ActiveRecord::Base
  validates_presence_of :attribute_one, :attribute_two
end

:

class MyModel < ActiveRecord::Base
  validates_presence_of :attribute_one
  validates_presence_of :attribute_two, :on => :update
end

Previously, my (passing) tests looked like this:

class MyModelTest < ActiveSupport::TestCase
  should_validate_presence_of :attribute_one, :attribute_two
end

As far as I can tell, should_validate_presence_ofthere is no parameter that will make this test continue to pass with the changes mentioned above. With the exception of the rejection of Shoulda when testing the requirement :attribute_two, is there any way to do this?

+5
source share
4 answers

In the past, I just used a small user block to block this problem:

should "require :attr_two on update" do
  mm = Factory(:my_model)
  mm.attr_two = nil
  mm.save
  assert_equal false, mm.valid?
  assert_equal("can't be blank", mm.errors.on(:attr_two))
 end

, , AR . , , .

+2

- ? ( shoulda-matchers-3.1.1)

subject { FactoryGirl.build(:your_model) }
it { is_expected.to validate_presence_of(:attribute_one) }
it { is_expected.to validate_presence_of(:attribute_two).on(:update) }
+3

, tsdbrown. , :
validates_presence_of: attr_two

, :
validates_presence_of: attr_two,: on = > : save

, : attr_two [] [ " " ]

+2

Rspec :

describe MyModelTest do
  describe "validations" do
    should_validate_presence_of :attribute_one

    context "on update" do
      subject { FactoryGirl.create(:my_model_test) }

      should_validate_presence_of :attribute_two
    end
  end
end
+2

All Articles