思考Sphinx,实时索引不使用FactoryGirl在RSpec中更新

时间:2016-09-07 09:26:57

标签: ruby-on-rails rspec thinking-sphinx

我正在使用Rspec和Capybara,我使用FG运行TS,但似乎从FG创建的模型在测试条件下没有填充TS。

## rails_helper.rb
RSpec.configure do |config|
  config.before(:each, sphinx: true) do |example|
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.start

    ThinkingSphinx::Test.init
    ThinkingSphinx::Test.start index: false
    ThinkingSphinx::Configuration.instance.settings['real_time_callbacks'] =
      (example.metadata[:type] == :request)
  end
  config.after(:each, sphinx: true) do |example|
    if example.metadata[:type] == :request
      ThinkingSphinx::Test.stop
      ThinkingSphinx::Test.clear
    end
  end
end

## some_request_spec.rb
feature 'SearchController#search', type: :request, sphinx: true do
  let!(:model) { create(:model) }
  scenario 'test' do
    # pry
    expect(Model.search('something').count).to eq 1
  end
end

## model_index.rb
ThinkingSphinx::Index.define :model, :with => :real_time do
  # fields
  indexes title

  has radian_latitude,  :as => :latitude,  :type => :float
  has radian_longitude, :as => :longitude, :type => :float
end

如果我在pry文件中的注释位置使用request_spec.rb,并在另一个控制台中运行以下代码

RAILS_ENV=test rake ts:generate

然后记录将注册并且测试将通过。

我也试过这个无济于事

ThinkingSphinx::Test.run do
  expect(Model.search('').count).to eq 1
end

不确定这里的配置是什么错误

更新

我在模型中添加了after_save ThinkingSphinx::RealTime.callback_for(:model)

然而,我收到了这个错误

NoMethodError: undefined method `find_all' for #<Model:0x007fa804700838>

当我在开发中播种数据库时发生这种情况。

这是堆栈跟踪:

NoMethodError: undefined method `find_all' for #<Model:0x007fda83782890>
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activemodel-4.2.6/lib/active_model/attribute_methods.rb:433:in `method_missing'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:276:in `associated_records_to_validate_or_save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:311:in `validate_collection_association'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:217:in `block in define_autosave_validation_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:157:in `instance_eval'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:157:in `block in define_non_cyclic_method'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_validate_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activemodel-4.2.6/lib/active_model/validations.rb:399:in `run_validations!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activemodel-4.2.6/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_validation_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activemodel-4.2.6/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activemodel-4.2.6/lib/active_model/validations.rb:338:in `valid?'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/validations.rb:58:in `valid?'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/validations.rb:83:in `perform_validations'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/validations.rb:37:in `save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block (2 levels) in save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:286:in `block in save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:285:in `save'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:413:in `save_has_one_association'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/autosave_association.rb:189:in `block in add_autosave_association_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `block in call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `each'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:506:in `call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_update_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/callbacks.rb:310:in `_update_record'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/timestamp.rb:70:in `_update_record'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/persistence.rb:504:in `create_or_update'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:117:in `call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:505:in `call'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_save_callbacks'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/callbacks.rb:302:in `create_or_update'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/persistence.rb:142:in `save!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/validations.rb:43:in `save!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:291:in `block in save!'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/transactions.rb:291:in `save!'
/Users/admin/Documents/Codigo/rails/myapp/db/seeds/30_food_seeds.rb:52:in `<top (required)>'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/admin/Documents/Codigo/rails/myapp/db/seeds.rb:3:in `block in <top (required)>'
/Users/admin/Documents/Codigo/rails/myapp/db/seeds.rb:3:in `each'
/Users/admin/Documents/Codigo/rails/myapp/db/seeds.rb:3:in `<top (required)>'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/railties-4.2.6/lib/rails/engine.rb:547:in `load_seed'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:248:in `block in execute'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:243:in `each'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:243:in `execute'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:187:in `block in invoke_with_call_chain'
/Users/admin/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:180:in `invoke_with_call_chain'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/task.rb:173:in `invoke'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:152:in `invoke_task'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:108:in `block (2 levels) in top_level'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:108:in `each'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:108:in `block in top_level'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:117:in `run_with_threads'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:102:in `top_level'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:80:in `block in run'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:178:in `standard_exception_handling'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/lib/rake/application.rb:77:in `run'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/bin/rake:23:in `load'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/bin/rake:23:in `<main>'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/bin/ruby_executable_hooks:15:in `eval'
/Users/admin/.rvm/gems/ruby-2.3.0@myapp/bin/ruby_executable_hooks:15:in `<main>'

0 个答案:

没有答案