【问题标题】:Testing Conditions with validate_uniqueness_of使用 validate_uniqueness_of 测试条件
【发布时间】:2017-02-22 11:22:46
【问题描述】:

类人:

class Person < ActiveRecord::Base
      validates_uniqueness_of :user_name, scope: :account_id
end

对于 Person 类,我可以使用 validate_uniqueness_of 测试方法对其进行测试,例如:

it { is_expected.to validate_uniqueness_of(:user_name).scoped_to(:account_id)




类文章:

class Article < ActiveRecord::Base
  validates_uniqueness_of :title, conditions: -> { where.not(status: 'archived') }
end

但是,对于在 documentation 中包含 conditions 的 Article 类,我似乎无法找到与 validate_uniqueness_of 等效的“should-matcher”测试方法。

我在谷歌上搜索过,但似乎仍然找不到解决方案。我是否必须为此编写自定义测试方法?

【问题讨论】:

  • 非常感谢任何能回答这个问题的人:)
  • 不行,手动写个测试吧。
  • @tpbowden,干杯。

标签: rspec ruby-on-rails-5 shoulda


【解决方案1】:

我们已经known 讨论了一段时间,但遗憾的是我们还没有解决这个问题。正如 tpbowden 所说,您必须手动对其进行测试。对不起:(

【讨论】:

  • 如果以后能加入就太好了!感谢您的回答。
  • 2020 年了,这个问题解决了吗?
  • @Mr. Rene 它没有,但我们很乐意通过 PR 来修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
相关资源
最近更新 更多