RSpec:带有searchkick的模型无效



我不太确定我做得对,但我有两个模型: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,我真的花了很多时间和精力来掌握它,非常感谢!

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

尝试在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. let相反,let!会立即被调用(从而在执行索引操作之前创建记录(
  2. 添加一个断点(IDE(或使用一个调试器(byebug、pry等(,并将其放在get :index调用之前,以查看已经存在的房屋(如果有的话(

最新更新