繁体   English   中英

RSpec:带有搜索踢的无效模型

[英]RSpec: invalid model with searchkick

我不太确定自己做对的地方,但是我有两个模型: has_one Address House

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开始,确实要花费我很多时间和精力来尝试掌握它,非常感谢!

尝试在before块中创建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. 相对于letlet! 立即被调用(从而在命中索引操作之前创建记录)
  2. 添加一个断点(IDE)或使用调试器(byebug,pry等),然后将其放在get :index调用之前,以查看已经存在的房屋(如果有)。

Searchkick 文档,有关为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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM