gpt4 book ai didi

ruby-on-rails - 将Elasticsearch与RSpec和Factorygirl一起使用

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

我找到了一篇很棒的文章,关于将Elasticsearch与RSpec here集成在一起,但是每当我使用Factorygirl创建模型时,似乎都不会添加到我的Elasticsearch索引中。关于如何使用Factorygirl向Elasticsearch添加模型的任何想法?

这是我的代码

display_controller_spec.rb:

    it "will return a list of displays", :elasticsearch do
FactoryGirl.create(:display, client: @client)

get :index, { :format => :json }, { "Accept" => "application/json" }
# returns Status: 200 OK
expect(response.status).to eq 200

json = JSON.parse(response.body)
expect(json.count).to be > 0
end

display.rb:
    require 'elasticsearch/model'

class Display < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name ['company', Rails.env, self.base_class.to_s.pluralize.underscore].join('_')
...

factory / displays.rb
FactoryGirl.define do
sequence(:display_name) {|n| "Display ##{n}"}

factory :display do
name { generate(:display_name) }
client
end
end

spec_helper.rb:
  config.before(:each) do
[Event, Interaction, Perch].each do |model|
model.__elasticsearch__.create_index!(force: true)
end
end

最佳答案

我有这个设置工作。我认为您只是缺少对Model.import的调用,以将记录导入elasticsearch中。您还需要确保任何工厂创建都可以进行!阻止,不在您的测试之内,以便导入将获取这些新记录。尝试这样的事情:

display_controller_spec.rb:

context "elasticsearch tests" do
let!(:display) { FactoryGirl.create(:display, client: @client) }

it "will return a list of displays", :elasticsearch do
get :index, { :format => :json }, { "Accept" => "application/json" }
# returns Status: 200 OK
expect(response.status).to eq 200

json = JSON.parse(response.body)
expect(json.count).to be > 0
end
end

spec_helper.rb:
RSpec.configure do |config|

...

ES_CLASSES = [Event, Interaction, Perch]

config.before :all do
ES_CLASSES.each do |esc|
esc.__elasticsearch__.client.indices.create(
index: esc.index_name,
body: { settings: esc.settings.to_hash, mappings: esc.mappings.to_hash }
)
end
end

config.after :all do
ES_CLASSES.each do |esc|
esc.__elasticsearch__.client.indices.delete index: esc.index_name
end
end

config.before(:each, elasticsearch: true) do
ES_CLASSES.each { |esc| esc.import(refresh: true, force: true) }
end
end

关于ruby-on-rails - 将Elasticsearch与RSpec和Factorygirl一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33641207/

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