ruby-on-rails - RSpec:带有 searchkick 的无效模型
问题描述
我不太确定我做得对,但我有两个模型:House
那个has_one Address
.
该Address
模型具有:
class Address < ApplicationRecord
searchkick
belongs_to :house
end
我正在尝试house_controller
像这样使用 RSpec测试我的
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
在 before 块中创建您的:
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
调用之前以查看哪些(如果有)房屋已经存在。
推荐阅读
- c# - 在地图图像上显示图钉
- outlook - “新 Outlook”的 Automator / Applescript - “选定对象”不再起作用
- flutter - 如何使用飞镖语言在字符串中搜索罗马数字?
- php - 试图访问对象的数据,说无法访问
- angular - 为什么打字稿在我的 IDE 中给我一个错误,说 Observable 上的属性不存在但显示它们存在?
- mysql - 没有中间表的sql连接(旧数据库)
- javascript - 如何从我的网站中的表单接收消息
- sql - SQL 仅从最大值中获取一列
- abap - 自定义字段未显示在表格输出中
- java - 如何在运行时将元素存储在 Array 中而不知道在 Java 中初始化期间的数组大小?你不能使用 Arraylist