ThinkingSphinx 实时索引和 Rspec 验收测试 w Chrome headless

问题描述 投票:0回答:1

继续:ThinkingSphinx 和回调在创建记录后不更新索引
我正在使用 rspec 和 chrome headless 测试搜索。
对于 SQL 索引,我使用了这个功能规范 + 帮助程序:https://pastebin.com/9m7WbvN7
用实时替换 SQL 索引:

ThinkingSphinx::Index.define :review, with: :real_time do
  indexes title, sortable: true
  indexes body
  indexes author.username, as: :author, sortable: true

  has author_id, :type => :integer
  has created_at, :type => :timestamp
  has updated_at, :type => :timestamp
end

在开发模式下,一切正常,但测试开始下降。
我尝试使用手册编辑 sphinx_helper:https://freelancing-gods.com/thinking-sphinx/v5/testing.html
但我失败了,得到一个错误:

An error occurred in a `before(:suite)` hook.
Failure/Error: ThinkingSphinx::Test.start_with_autostop
Riddle::CommandFailedError:
  Sphinx command failed to execute

# /gems/riddle-2.4.3/lib/riddle/execute_command.rb:25:in `call'
# /gems/riddle-2.4.3/lib/riddle/execute_command.rb:7:in `call'
# /gems/riddle-2.4.3/lib/riddle/controller.rb:33:in `index'
# /gems/thinking-sphinx-5.5.1/lib/thinking_sphinx/test.rb:13:in `start'
# /gems/thinking-sphinx-5.5.1/lib/thinking_sphinx/test.rb:19:in `start_with_autostop'
# ./spec/sphinx_helper.rb:14:in `block (2 levels) in <top (required)>'

更新:添加端口并没有改变任何东西。
配置/thinking_sphinx.yml:

test:
  mysql41: 9307

如果你通过 rake 手动运行它,配置文件被创建并且 sphinx 运行成功:

bundle exec rake ts:configure RAILS_ENV=test
Generating configuration to /home/vagrant/data/app/config/test.sphinx.conf

bundle exec rake ts:start RAILS_ENV=test
using config file '/home/vagrant/data/app/config/test.sphinx.conf'...
listening on 127.0.0.1:9307
precaching index 'review_core'
precaching index 'user_core'
precached 2 indexes in 0.002 sec
Started searchd successfully (pid: 6532).

test.sphinx.conf的内容和development.sphinx.conf一样,只是路径变了
顺便说一句,test.sphinx.conf 是在尝试运行测试时创建的https://pastebin.com/GavMg5BB
如果加上

/gems/thinking-sphinx-5.5.1/lib/thinking_sphinx/test.rb

def self.start(options = {})
  pp  config.controller

输出:https://pastebin.com/ta34Ys6k
运行测试时,log/test.searchd.log 中没有任何内容,因为 sphinx 没有启动。

UP2 这就是谜语试图运行的内容::

# /gems/riddle-2.4.3/lib/riddle/execute_command.rb:7
"indexer --config \"/home/vagrant/data/app/config/test.sphinx.conf\" --all"

如果我尝试手动执行,我会得到:

ERROR: nothing to do.

ruby-on-rails rspec full-text-search thinking-sphinx acceptance-testing
1个回答
1
投票

万岁,我终于明白了。
是的

start_with_autostop
(不仅如此)调用索引,它返回 Riddle 错误。
我们也不需要打电话
ThinkingSphinx::Test.index

ThinkingSphinx::Test.run
,用于包装测试,也不再对我们有用,因为它调用
ThinkingSphinx::Test.start
没有参数
index: false
.

现在我们只需要运行 Sphinx 并在模型中使用 ThinkingSphinx 回调,否则您创建的对象将不会被索引,这是有道理的。
由于我们现在在模型上有回调,我们必须在使用这些模型的所有测试中保持 Sphinx 运行,否则我们会得到一个错误:

Error connecting to Sphinx via the MySQL protocol. Can't connect to MySQL server on '127.0.0.1:9307' (111).

所以我们需要在整个测试期间在后台运行 Sphinx,例如Redis
如果我们使用每个规范文件启动和停止 Sphinx,那么我们的测试时间将显着增加。


目前有几种解决方案对我有用:

1。添加到 RSpec.configure 中的 rails_helper

  # Starting Sphinx before all tests
  config.before(:suite) do
    ThinkingSphinx::Test.init
    ThinkingSphinx::Test.start(index: false)
  end

  # Stoping ThinkingSphinx after all the tests
  config.after(:suite) do
    ThinkingSphinx::Test.stop
  end

接下来是您的选择:

  1. 删除 DatabaseCleaner 和 sphinx_helper(Rails 6 没有它们似乎一切正常,也许我使用和使用 DatabaseCleaner 的手册已经过时了)。
  2. 保留 DatabaseCleaner 和 sphinx_helper,但删除其中的
    ThinkingSphinx
    调用,sphinx_helper 在 Sphinx 的特性测试中调用,而不是 rails_helper。

2。通过 rails_helper 中的 Rake 任务启动 Sphinx:

  # before RSpec.configure
  Rails.application.load_tasks
    
  # inside RSpec.configure
  config.before(:suite) do
    Rake::Task['ts:configure'].invoke
    Rake::Task['ts:start'].invoke
  end

  config.after(:suite) do
    Rake::Task['ts:stop'].invoke
    Rake::Task['ts:clear'].invoke
  end

接下来是您的选择:与第 1 点相同。

3。如果你需要DatabaseCleaner,你可以将它全局放在RSpec.configure中的rails_helper中:

  config.use_transactional_fixtures = false

  # Starting Sphinx before all tests
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    ThinkingSphinx::Test.init
    ThinkingSphinx::Test.start(index: false)
  end

  config.before(:each) do |test|
    # fix ActiveRecord::RecordNotFound in JS tests
    DatabaseCleaner.strategy = test.metadata[:js] ? :truncation : :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  # Stoping ThinkingSphinx after all the tests.
  config.after(:suite) do
    ThinkingSphinx::Test.stop
  end

sphinx_helper 然后删除它。


在所有功能测试 Sphinx 中,您需要删除

ThinkingSphinx::Test.run
包装器,以及
sphinx: true
js: true
(除非您明确需要 JS)
另外,不要忘记向索引模型添加回调:

  ThinkingSphinx::Callbacks.append(
    self, :behaviours => [:real_time]
  )

您还可以在所有测试后添加删除索引文件夹并停止 Sphinx:

  config.after(:suite) do
    ThinkingSphinx::Test.stop
    FileUtils.rm_rf("#{Rails.root}/db/sphinx/test")
  end

PS:如果有新的细节,我会更新帖子。

© www.soinside.com 2019 - 2024. All rights reserved.