应该:测试 validates_presence_of :on => :更新

发布于 2024-08-16 11:47:22 字数 667 浏览 1 评论 0原文

我在我从事的一个项目中将 Shoulda 与 Test::Unit 结合使用。我遇到的问题是我最近将其更改为:

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

,我的(通过)测试看起来像这样:

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

据我所知, should_validate_presence_of 没有参数可以使此测试继续通过上面指定的更改。在测试 :attribute_two 的要求时,没有放弃 Shoulda,有什么办法可以解决这个问题吗?

I'm using Shoulda in combination with Test::Unit on one of the projects I work on. The issue I'm running into is that I recently changed this:

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

to this:

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, there is no parameter to should_validate_presence_of that will cause this test to continue to pass with the changes specified above. Short of abandoning Shoulda when testing the requirement of :attribute_two, is there any way around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烛影斜 2024-08-23 11:47:22

像这样的事情怎么办? (对于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) }

What about something like this? (for 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) }
爱,才寂寞 2024-08-23 11:47:22

过去,我只是使用了一个小的自定义 should 块来解决这个问题:

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

希望 shoulda 将来能够通过允许进一步的 AR 验证选项来不断改进。让我知道你的想法,干杯。

In that past I have just used a small custom should block to get around 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

Hopefully shoulda will keep improving by allowing further AR validation options in the future. Let me know what you think, cheers.

梦归所梦 2024-08-23 11:47:22

在 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

In Rspec you can do the following:

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
我家小可爱 2024-08-23 11:47:22

我尝试了类似于 tsdbrown 建议的解决方案。当我有以下情况时,这种类型的测试会通过:

validates_presence_of :attr_two

但是如果我将模型更改为:
则测试会失败:

validates_presence_of :attr_two, :on => :save

它会失败,因为 :attr_two 错误是 [] 而不是 ["can't be Blank"]

I have tried a solution similar to what tsdbrown suggested. This type of test passes when I have:

validates_presence_of :attr_two

But the test fails if I change the model to:

validates_presence_of :attr_two, :on => :save

It fails because the :attr_two error is [] instead of ["can't be blank"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文