【问题标题】:Testing a variable scope association测试变量范围关联
【发布时间】:2015-09-02 02:07:27
【问题描述】:

我正在努力使我的测试变得健壮且非常可靠,并且我一直在将一些复杂的查询和关联分解为更小的查询和关联,或者重构数据并将其移动到范围中。

给定以下类:

class Item < ActiveRecord::Base
  belongs_to :location

  scope :in_location, ->(location) { where(location: location) }
  scope :findable,    ->(location, not_ids) {
    in_location(location).where.not(id: not_ids)
  }
end

class Container < ActiveRecord::Base
  belongs_to :location
  # THIS IS WHAT I WANT TO TEST 
  has_many :findable_items, ->(container) { 
    findable(container.location, container.not_findable_ids)
  }, class_name: 'Item'
end

您将如何测试像这样的变量 has_many 关系而不会在很大程度上影响数据库?我知道我可以自己测试 Item.findable 方法;我感兴趣的是 container.findable_items 方法。

注意:被测试的实际关联比这更复杂,并且需要相当广泛的设置;它通过其他一些嵌套关联和范围运行。如果可能的话,我想避免这种设置,只是测试使用正确的值调用范围。

我的 Gemfile 的相关部分:

rails (4.2.3)
shoulda-matchers (2.6.2)
factory_girl (4.5.0)
factory_girl_rails (4.5.0)
rspec-core (3.3.2)
rspec-expectations (3.3.1)
rspec-its (1.2.0)
rspec-mocks (3.3.2)
rspec-rails (3.3.3)

我的项目中有 shoulda-matchers,所以我可以进行基本的健全性测试:

it { should have_many(:findable_items).class_name('Item') }

但这失败了:

describe 'findable_line_items' do
  let(:container) { @container } # where container is a valid but unsaved Container
  let(:location)  { @container.location }
  it 'gets items that are in the location and not excluded' do
    container.not_findable_ids = [1,2]
    # so it doesn't hit the database
    expect(Item).to receive(:findable).with(location, container.not_findable_ids) 
    container.findable_items
  end
end

此规范失败并出现以下错误:

1) Container findable_line_items gets items that are in the location and not excluded
 Failure/Error: container.findable_items
 NoMethodError:
   undefined method `except' for nil:NilClass
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:158:in `block (2 levels) in add_constraints'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:154:in `each'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:154:in `block in add_constraints'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:141:in `each'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:141:in `each_with_index'
 # /[redacted]/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:141:in `add_constraints'
 # /[redacted]/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:39:in `scope'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association_scope.rb:5:in `scope'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association.rb:97:in `association_scope'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/association.rb:86:in `scope'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/collection_association.rb:423:in `scope'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/collection_proxy.rb:37:in `initialize'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:106:in `new'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/relation/delegation.rb:106:in `create'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/collection_association.rb:39:in `reader'
 # /[redacted]/gems/activerecord-4.2.3/lib/active_record/associations/builder/association.rb:115:in `pickable_items'
 # ./spec/models/container_spec.rb:25:in `block (3 levels) in <top (required)>'

在不实际设置满足所有要求的项目的情况下,如何通过此规范?

【问题讨论】:

  • 剔除数据库在很多情况下是一个好主意,但不是在这里,因为 Rails 中的作用域和关系都与 ORM / db 有着错综复杂的联系。即使有可能如果您对实际功能进行存根,您的测试实际上会给出什么值?
  • 我正在 item_spec 的测试中测试“实际”功能;我不需要测试范围是否在这里工作,只是它被调用了。

标签: ruby-on-rails rspec shoulda


【解决方案1】:

我最终采用了这样的解决方案:

describe 'findable_line_items' do
  let(:container) { @container } # where container is a valid but unsaved Container
  let(:location)  { @container.location }
  it 'gets items that are in the location and not excluded' do
    # so it doesn't hit the database
    expect(Item).to receive(:findable).with(location, container.not_findable_ids).and_call_original
    expect(container).to receive(:location).and_call_original
    expect(container).to receive(:not_findable_ids).and_call_original
    container.findable_items
  end
end

发生的错误是在 ActiveRecord 关联设置中的某处;它试图在从我的 Item 存根返回的 nil 对象上实例化一个 ActiveRecord 数组。添加.and_call_original 解决了这个错误。

我并不真正关心测试是否从该关联返回了正确的对象,因为该范围正在其他地方进行测试,只是该范围正在被使用。在这种情况下,它仍然会访问数据库,但不是设置完整测试所需的 15 次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多