【问题标题】:Testing constraints on has_many with rspec in Rails在 Rails 中使用 rspec 测试 has_many 的约束
【发布时间】:2013-11-08 11:03:48
【问题描述】:

我正在使用 shoulda-matchers 和 rspec 来测试我的 Rails 模型上的 has_many 关系。例如:

class Parent
  has_many :children
  has_many :other_children, :through => :intermediate
end

# spec/models/parent_spec.rb
describe Parent do
  it "has children" do
    should have_many(:children)
    should have_many(:other_children).through(:intermediate)
  end
end

一切正常。但是我如何最好地测试这些关系的额外约束,例如何时存在:

has_many :children, :dependent => :destroy
has_many :children, :uniq => true
has_many :children, :through => :intermediate, :source => :partner

【问题讨论】:

  • Shoulda-matchers 现在是 have dependentthrough 匹配器。我没有看到uniq,但是您可以轻松查看源代码并以相同的方式执行您想要的操作。

标签: ruby-on-rails rspec shoulda


【解决方案1】:

我正在搜索相同的内容,发现 there are 为不同的限定符定义了匹配器,包括:dependent、:through 和 :source,但是对于 :uniq,我没有找到... 也许是因为它在 Rails 4 中已被弃用...

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多