【问题标题】:RSpec: invalid model with searchkickRSpec:带有 searchkick 的无效模型
【发布时间】:2018-11-26 21:49:31
【问题描述】:

我不太确定自己做得对,但我有两个模型:Househas_one Address

Address 模型具有:

class Address < ApplicationRecord
  searchkick
  belongs_to :house
end

我正在尝试像这样使用 RSpec 测试我的house_controller

RSpec.describe HousesController do
 context 'GET #index' do
 before { get :index }
 it     { is_expected.to render_template('index') }

 it 'assigns @houses' do
  h = create(:house)
  expect(assigns(:houses).results).to eq([h])
end
...

尽管如此,我总是得到一个不是我期望的结果。 我的控制器的代码如下:

def index
 if params[:term].present?
  @houses = House.search(params[:term])
 else
  @houses = House.search('*')
 end
end

我不确定我是否理解它,但可能是因为我使用FactoryBot,它正在创建很多房屋,然后当进入index 方法时,那里有一堆房屋不仅仅是h?

这是我的失败:

Failures:

  1) HousesController GET #index assigns @houses
     Failure/Error: expect(assigns(:houses).results).to eq([h])

       expected: [#<House id: 763, rent: 1173, deposit: 739, description: "Rerum cado curso curo alias.", preferred_ge...2018-11-26 21:40:43", available_at: "2018-12-17", user_id: 15945, lease_length: nil, built_in: nil>]
            got: [#<House id: 215, rent: 0.839e3, deposit: 0.797e3, description: "Rerum aeneus taceo crepusculum aestu...2018-11-26 21:17:53", available_at: "2018-12-17", user_id: 15776, lease_length: nil, built_in: nil>]

       (compared using ==)

       Diff:
       @@ -1,2 +1,5 @@
       -[#<House id: 763, rent: 1173, deposit: 739, description: "Rerum cado curso curo alias.", preferred_gender: 0, created_at: "2018-11-26 21:40:43", updated_at: "2018-11-26 21:40:43", available_at: "2018-12-17", user_id: 15945, lease_length: nil, built_in: nil>]
       +[#<House id: 215, rent: 0.839e3, deposit: 0.797e3, description: "Rerum aeneus taceo crepusculum aestus.", preferred_gender: 0, created_at: "2018-11-25 12:50:11", updated_at: "2018-11-25 12:50:11", available_at: "2018-12-16", user_id: 8065, lease_length: nil, built_in: nil>,
       + #<House id: 235, rent: 0.519e3, deposit: 0.642e3, description: "Cicuta totidem arbustum arcesso fugit tego.", preferred_gender: 0, created_at: "2018-11-25 12:54:28", updated_at: "2018-11-25 12:54:28", available_at: "2018-12-16", user_id: 8085, lease_length: nil, built_in: nil>,
       + #<House id: 648, rent: 0.668e3, deposit: 0.1104e4, description: "Corporis tametsi demens.", preferred_gender: 0, created_at: "2018-11-26 21:17:43", updated_at: "2018-11-26 21:17:43", available_at: "2018-12-17", user_id: 15775, lease_length: nil, built_in: nil>,
       + #<House id: 649, rent: 0.799e3, deposit: 0.611e3, description: "Ut ancilla tredecim.", preferred_gender: 0, created_at: "2018-11-26 21:17:53", updated_at: "2018-11-26 21:17:53", available_at: "2018-12-17", user_id: 15776, lease_length: nil, built_in: nil>]

     # ./spec/controllers/houses_controller_spec.rb:12:in `block (3 levels) in <top (required)>'

我现在开始使用 RSpec,我真的需要花费大量时间来尝试掌握它,提前非常感谢!

【问题讨论】:

  • 您希望它有一个结果还是多个结果?是的,看起来你正在其他地方建造更多的房子

标签: ruby-on-rails testing rspec factory-bot searchkick


【解决方案1】:

Searchkick docs 关于禁用 RSpec 测试的索引。

您不希望在运行测试时始终更新 Elasticsearch 中的对象。只有在明确测试搜索功能(或索引/从索引中删除)时,您才想这样做。为此,您必须禁用 searchkick 回调,为您的测试定义自定义标签并仅为这些测试启用索引。您可能还必须在测试/测试组之后清理索引。

@vich 的观点也很重要,您目前创建对象太晚了,在请求之后。

我会将您的设置更改为:

context 'GET #index', :search do
  let!(:house) { create(:house) }
  before { get :index }

  it 'assigns @houses' do
    expect(assigns(:houses).results).to eq([house])
  end
end

【讨论】:

  • 感谢您的详细回复!不幸的是,House 表中似乎没有任何房子
  • 它们不在表中,您的数据库在测试之间被正确清除。每次您创建房子时,它都会在您的 Elasticsearch 中被索引,而不会被清除。如果您按照我链接的文档中的说明进行操作,您将能够正确配置。
  • 确实这是解决方案,非常感谢!作为一个新手,我有时很难知道究竟是什么修复了它,以及为什么(searchkick github 网站上没有太多解释)
【解决方案2】:

尝试在前面的块中创建您的house

context 'GET #index' do
  before do
    let!(:house) { create(:house) }
    get :index
  end

  it { is_expected.to render_template('index') }

  it 'assigns @houses' do
    expect(assigns(:houses).results).to eq([house])
  end
end

有几点需要注意:

  1. let 不同,let! 会立即被调用(从而在索引操作被命中之前创建您的记录)
  2. 添加断点 (IDE) 或使用调试器(byebug、pry 等)并将其放在 get :index 调用之前,以查看已存在哪些(如果有)房屋。

【讨论】:

  • 我试过byebugHouse.all 显示一个空的ActiveRecord::RelationHouse.count 显示0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
相关资源
最近更新 更多