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

Ruby on rails RSpec:带有searchkick的模型无效,ruby-on-rails,testing,rspec,factory-bot,searchkick,Ruby On Rails,Testing,Rspec,Factory Bot,Searchkick,我不太确定我是否做对了,但我有两种型号:房子只有一个地址 地址型号有: class Address < ApplicationRecord searchkick belongs_to :house end 然而,我总是得到一个不是我期望的结果。 我的控制器的代码如下所示: def index if params[:term].present? @houses = House.search(params[:term]) else @houses = House.sear

我不太确定我是否做对了,但我有两种型号:
房子
只有一个地址

地址
型号有:

class Address < ApplicationRecord
  searchkick
  belongs_to :house
end
然而,我总是得到一个不是我期望的结果。 我的控制器的代码如下所示:

def index
 if params[:term].present?
  @houses = House.search(params[:term])
 else
  @houses = House.search('*')
 end
end
我不确定我是否理解它,但这可能是因为我使用的是
FactoryBot
,它正在创建许多房屋,然后当使用
索引
方法时,那里有一堆房屋,而不仅仅是
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)>'
故障:
1) HousesController获取#索引分配@houses
失败/错误:expect(分配(:house).results)。到等式([h])

应为:[#尝试在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
    调用之前,以查看哪些(如果有的话)房屋已经存在
  • 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
    

    你期望它有一个或多个结果吗?是的,看起来你在其他地方创建了更多的房子。我尝试了
    byebug
    但是
    house。所有的
    都显示了一个空的
    ActiveRecord::Relation
    house。count
    表示0感谢详细的响应!不幸的是中似乎没有任何房子ode>House
    table它们不在表中,您的数据库在测试之间被正确地清除。每次您创建一个House时,它都会在您的Elasticsearch中被索引,而Elasticsearch不会被清除。如果您按照我链接的文档中的说明进行操作,您将能够正确地配置它。事实上,这就是解决方案,谢谢很多!作为一个新手,我有时很难知道到底是什么修复了它,为什么(searchkick github网站上没有太多解释)
    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