首页 > 解决方案 > 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 开始,尝试掌握它真的需要花费我的精力和时间,在此先感谢!

标签: ruby-on-railstestingrspecfactory-botsearchkick

解决方案


尝试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

有几点需要注意:

  1. 与 , 相反letlet!立即调用(因此在点击索引操作之前创建您的记录)
  2. 添加断点 (IDE) 或使用调试器(byebug、pry 等)并将其放在get :index调用之前以查看哪些(如果有)房屋已经存在。

推荐阅读