耐嚼RSpec测试 - 预期索引`客户#person_client`将被更新,但事实并非如此

时间:2015-04-14 19:50:43

标签: ruby-on-rails rspec elasticsearch

我正在使用Chewy在Rails应用程序中与ElasticSearch进行交互。我正在尝试将RSpec测试添加到我的索引(ClientIndex),但我认为我在这里做错了。

我的模型Client

class Client < ActiveRecord::Base
  update_index('client#person_client') { self }
end

我的ClientIndex

class ClientIndex < Chewy::Index
  settings analysis: {
   # ...
  }

  define_type Client, name: 'person_client' do
    field :id, value: ->(client) { client.id }
    field :name, search_analyzer: 'str_search_analyzer', index_analyzer: 'index_analyzer'

    field :name_sorted, value: ->(client) { client.name }
    field :email, search_analyzer: 'str_search_analyzer', index_analyzer: 'index_analyzer'

    field :created_at, type: 'date'
  end
end

我的RSpec测试:

RSpec.describe ClientIndex do
  it "update index after save" do
    client = Fabricate.build(:client, id: 10)
    expect { client.save! }.to update_index('client#person_client')
  end
end

结果:

Failures:

  1) ClientIndex update index after save
     Failure/Error: expect { client.save! }.to update_index('client#person_client')
       Expected index `client#person_client` to be updated, but it was not

这里有什么建议吗?

由于

1 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。在spec_helper.rb中,我不得不选择耐嚼的指数化策略。我说:

Chewy.root_strategy = :urgent

现在有效。

如果你没有选择索引策略,它会在回调中引发错误,但是当你在对象上创建它时它是静默的。

我希望这会有所帮助。