【问题标题】:How to Test Elasticsearch and Searchkick with RSpec如何使用 RSpec 测试 Elasticsearch 和 Searchkick
【发布时间】:2014-05-14 03:10:24
【问题描述】:

感谢您查看我的问题。我在通过测试时一直很失败,所以我求助于 StackOverflow 的优秀人员。任何帮助都会很重要!我已经按照这个答案来帮助设置我的测试:

Testing searchkick with RSpec

这是我失败的测试

provider_controller_spec.rb

describe "#set_locations" do

  let(:provider) { create(:provider) }

  before(:each) { login(provider) }

  context "with search params" do

    let(:location) { create(:location) }
    let(:business) { create(:business, locations: [location]) }

    before(:each) do
      Business.searchkick_index.refresh
      get :set_locations, locale: "es", id: provider.url , query: business.commercial_name
    end

    it "should return search results" do
      expect(assigns(:businesses)).to eq([business])
    end
  end
end

我收到以下错误:

Failures:

  1) ProvidersController#set_locations with search params should return search results
     Failure/Error: expect(assigns(:businesses)).to eq([business])

Diff:
   @@ -1,2 +1,55 @@
   -[#<Business id: 99, email: nil, commercial_name: "Cazares Ledesma Hermanos", ruc: nil, phone: nil, razon_social: nil, website: nil, account_type: nil, created_at: "2014-04-02 16:42:26", updated_at: "2014-04-02 16:42:26", subcategory_id: nil, password_digest: nil, token: "0tw3uj7oy7q", slogan: nil, description: nil>]

   +#<Searchkick::Results:0x007f99420d7a18
   + @facets=nil,
   + @max_score=0.0,
   + @options=
   +  {:load=>true,
   +   :payload=>
   +    {:query=>
   +      {:dis_max=>
   +        {:queries=>
   +          [{:multi_match=>
   +             {:fields=>["_all"],
   +              :query=>"Cazares Ledesma Hermanos",
   +              :use_dis_max=>false,
   +              :operator=>"and",
   +              :boost=>10,
   +              :analyzer=>"searchkick_search"}},
   +           {:multi_match=>
   +             {:fields=>["_all"],
   +              :query=>"Cazares Ledesma Hermanos",
   +              :use_dis_max=>false,
   +              :operator=>"and",
   +              :boost=>10,
   +              :analyzer=>"searchkick_search2"}},
   +           {:multi_match=>
   +             {:fields=>["_all"],
   +              :query=>"Cazares Ledesma Hermanos",
   +              :use_dis_max=>false,
   +              :operator=>"and",
   +              :fuzziness=>1,
   +              :max_expansions=>3,
   +              :analyzer=>"searchkick_search"}},
   +           {:multi_match=>
   +             {:fields=>["_all"],
   +              :query=>"Cazares Ledesma Hermanos",
   +              :use_dis_max=>false,
   +              :operator=>"and",
   +              :fuzziness=>1,
   +              :max_expansions=>3,
   +              :analyzer=>"searchkick_search2"}}]}},
   +     :size=>100000,
   +     :from=>0,
   +     :fields=>[]},
   +   :size=>100000,
   +   :from=>0,
   +   :term=>"Cazares Ledesma Hermanos"},
   + @response=
   +  {"took"=>2,
   +   "timed_out"=>false,
   +   "_shards"=>{"total"=>1, "successful"=>1, "failed"=>0},
   +   "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}},
   + @results=[],
   + @time=2,
   + @total=0,
   + @wrapper=Tire::Results::Item>

这是我正在测试的内容:

providers_controller.rb

def set_locations
  @businesses = Business.search(params[:query], page: params[:page]) if params[:query].present?
end

企业.rb

class Business < ActiveRecord::Base
  include Tokenable
  searchkick language: "Spanish"
  searchkick autocomplete: ['commercial_name']
  searchkick word_start: [:name]
  searchkick settings: {number_of_shards: 1}

  validates_presence_of :commercial_name

  has_many :locations, :dependent => :destroy

  accepts_nested_attributes_for :locations,
    reject_if: lambda { |a| a[:street_address].blank? },
    :allow_destroy => true
end

我将此添加到 spec_helper.rb

config.before :each do
  Business.reindex
end

【问题讨论】:

    标签: ruby-on-rails rspec elasticsearch


    【解决方案1】:

    试试:

    expect(assigns(:businesses).results).to eq([business])
    

    此外,在您的业务模型中,结合您的 searchkick 调用:

    searchkick language: "Spanish",
               autocomplete: ['commercial_name'],
               word_start: [:name],
               settings: {number_of_shards: 1}
    

    (单独调用会产生不良结果,这将在 Searchkick 的下一个版本中引发错误)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 2018-11-06
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 2016-02-24
      • 2017-03-22
      • 2015-05-11
      相关资源
      最近更新 更多