gpt4 book ai didi

ruby-on-rails - 如何使用RSpec测试Elasticsearch和Searchkick

转载 作者:行者123 更新时间:2023-12-02 22:25:40 24 4
gpt4 key购买 nike

感谢您看我的提问者。我在通过测试方面一直很失败,因此我转向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>

这是我正在测试的内容:

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

business.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])

另外,在您的业务模型中,组合您的搜寻踢 call :
searchkick language: "Spanish",
autocomplete: ['commercial_name'],
word_start: [:name],
settings: {number_of_shards: 1}

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

关于ruby-on-rails - 如何使用RSpec测试Elasticsearch和Searchkick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22818160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com