[英]How to Test Elasticsearch and Searchkick with RSpec
謝謝你們看我的問題。 我一直在讓我的測試通過失敗,所以我轉向 StackOverflow 的優秀人員。 任何幫助都會很棒! 我已經按照這個答案來幫助設置我的測試:
這是我失敗的測試
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.