繁体   English   中英

如何使用 RSpec 测试 Elasticsearch 和 Searchkick

[英]How to Test Elasticsearch and Searchkick with RSpec

谢谢你们看我的问题。 我一直在让我的测试通过失败,所以我转向 StackOverflow 的优秀人员。 任何帮助都会很棒! 我已经按照这个答案来帮助设置我的测试:

使用 RSpec 测试 searchkick

这是我失败的测试

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>

这是我正在测试的内容:

provider_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

尝试:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM