代码之家  ›  专栏  ›  技术社区  ›  noloman amram99

RSpec:带有searchkick的模型无效

  •  1
  • noloman amram99  · 技术社区  · 6 年前

    House 那个 has_one Address .

    Address 模型有:

    class Address < ApplicationRecord
      searchkick
      belongs_to :house
    end
    

    我在试着测试我的能力 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,我花了很多时间和精力去掌握它,提前多谢!

    2 回复  |  直到 6 年前
        1
  •  1
  •   Marcin Kołodziej    6 年前

    搜索踢 docs 关于使用RSpec禁用测试索引。

    @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
    
        2
  •  1
  •   vich    6 年前

    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. 相对于 let let! 立即调用(从而在执行索引操作之前创建记录)
    2. 添加断点(IDE)或使用调试器(byebug、pry等)并将其置于 get :index 打电话查看已有哪些房屋(如有)。