【问题标题】:RSpec and ThinkingSphinx returns no resultsRSpec 和 ThinkingSphinx 不返回任何结果
【发布时间】:2015-03-07 00:03:23
【问题描述】:

使用 FactoryGirl 创建记录,但该记录类的 search() 方法不返回任何内容。我可以使用 find() 确认该记录确实存在于数据库中。

我发现似乎没有任何效果。已尝试按照标准 thinking_sphinx 和 rspec 文档进行设置。包括我的 rails_helper、spec_helper、spec 和它的输出。非常感谢任何人的指导。(注意:我从这些文件中删除了相当多的内容,以留下足以重现错误的内容)

规格...

require 'rails_helper'

RSpec.describe SiteController do

  before(:each) do
    @article = create(:article)
  end

  describe "GET index" do
    it "assigns and renders articles" do
      get :index
      assert_equal assigns(:articles), [@article]
    end
  end
end

这是输出...

1) SiteController GET index assigns and renders articles
Failure/Error: assert_equal assigns(:articles), [@article]
Minitest::Assertion:
--- expected
+++ actual
@@ -1 +1 @@
-[]
+[#<Article id: 1, title: "Article Title", header_one: "Article      Header One", header_two: "Article Header Two", url: "http://www.testurl.com", description: "This is a test article!", body: "This is the article body.", photo_file_name: "test_image.png", photo_content_type: "image/png", photo_file_size: 96884, photo_updated_at: "2015-03-06 23:55:39", delta: true, user_id: 1, category_id: nil, posted_at: nil, created_at: "2015-03-06 23:55:40", updated_at: "2015-03-06 23:55:40">]

rails_helper.rb...

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!

  config.include FactoryGirl::Syntax::Methods
end

spec_helper.rb...

require 'devise'

module SphinxHelpers
  def index
    ThinkingSphinx::Test.index
    # Wait for Sphinx to finish loading in the new index files.
    sleep 0.25 until index_finished?
  end

  def index_finished?
    Dir[Rails.root.join(ThinkingSphinx::Test.config.indices_location, '*.{new,tmp}*')].empty?
  end
end

RSpec.configure do |config|

  config.raise_errors_for_deprecations!

  config.include Devise::TestHelpers, :type => :controller
  config.include SphinxHelpers

  config.before(:suite) do

    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)

    ThinkingSphinx::Test.init
    ThinkingSphinx::Test.start_with_autostop
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

  config.before(:each) do |example|
    index # if example.metadata[:js]
  end

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

【问题讨论】:

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


    【解决方案1】:

    感谢@pat 朝着正确的方向前进。该问题确实与以下有关

    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
    

    现在将其更改为以下内容似乎可以正常工作

    DatabaseCleaner.strategy = :truncation
    #DatabaseCleaner.clean_with(:truncation)
    

    (令人震惊的是,声明交易的行与我的交易问题有关:))

    我最初从数据库清理器 rspec 指令中提取了这些行,并认为 clean_with 声明是使用清理块时使用的声明。显然不明白那里的东西,但可以单独调查。谢谢!

    【讨论】:

      【解决方案2】:

      您不会在规范中的任何地方调用 index(来自 SphinxHelpers) - 因此,Sphinx 永远不会更新您的新记录。

      before(:each) 块的末尾添加index,看看是否有帮助。

      【讨论】:

      • 嘿,拍拍,谢谢回复。我自己也有同样的想法,但是应该通过 spec_helper 中的 before(:each) 为每个规范调用 index()?使用调试器跟踪整个测试运行确认我正在命中索引。甚至尝试在 create() 之后使用会思考的 sphinx 辅助方法显式调用 index。在查看日志时,测试似乎仍在事务中运行,但无法弄清楚为什么仍然会发生这种情况。
      • 这行可能是罪魁祸首:DatabaseCleaner.strategy = :transaction。另外:index 需要在您创建对象后调用 - 所以 spec_helper 中的 before(:each) 块不会有帮助,因为我希望它会被调用在每个测试特定的 before(:each) 块之前。
      • 增量索引应该处理在显式索引之后发生的任何更改。无论如何,我尝试在整个代码中放置索引调用,但没有成功。同意交易声明看起来很可疑,但不幸的是,更改它并没有任何影响。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 2017-12-09
      • 2020-10-03
      • 2017-03-19
      • 2017-05-20
      • 2012-08-08
      • 1970-01-01
      相关资源
      最近更新 更多