gpt4 book ai didi

ruby-on-rails - RSpec:带有 searchkick 的无效模型

转载 作者:行者123 更新时间:2023-11-28 20:26:40 24 4
gpt4 key购买 nike

我不太确定我做对了,但我有两个模型:House has_one 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
...

然而,我总是得到一个不是我期望的结果。我的 Controller 的代码如下:

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

我不确定我是否理解它,但可能是因为我正在使用 FactoryBot,它正在创建很多房屋,然后在进入 index 时code> 方法,那里有一堆房子,而且不仅是 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 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

关于ruby-on-rails - RSpec:带有 searchkick 的无效模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com