RSpec:带有 searchkick 的无效模型
RSpec: invalid model with searchkick
我不太确定我做对了,但我有两个模型:House
has_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 开始,我真的花了很多时间和精力来掌握它,非常感谢!
尝试在前面的块中创建您的 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
有几点需要注意:
- 与
let
相反,let!
被立即调用(因此在索引操作被命中之前创建您的记录)
- 添加断点(IDE)或使用调试器(byebug、pry 等)并将其放在
get :index
调用之前以查看已经存在的房屋(如果有)。
Searchkick docs 关于禁用 RSpec 测试的索引。
您不希望在 运行 测试时总是更新 Elasticsearch 中的对象。只有当您明确测试搜索功能(或 indexing/deleting 来自索引)时,您才想这样做。为此,您必须禁用 searchkick 回调,为您的测试定义一个自定义标签,并仅为这些测试启用索引。您可能还需要在 test/groups 测试后清理索引。
@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
has_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 开始,我真的花了很多时间和精力来掌握它,非常感谢!
尝试在前面的块中创建您的 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
有几点需要注意:
- 与
let
相反,let!
被立即调用(因此在索引操作被命中之前创建您的记录) - 添加断点(IDE)或使用调试器(byebug、pry 等)并将其放在
get :index
调用之前以查看已经存在的房屋(如果有)。
Searchkick docs 关于禁用 RSpec 测试的索引。
您不希望在 运行 测试时总是更新 Elasticsearch 中的对象。只有当您明确测试搜索功能(或 indexing/deleting 来自索引)时,您才想这样做。为此,您必须禁用 searchkick 回调,为您的测试定义一个自定义标签,并仅为这些测试启用索引。您可能还需要在 test/groups 测试后清理索引。
@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