RSpec和ThinkingSphinx没有返回任何结果

时间:2015-03-07 00:03:23

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

使用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

2 个答案:

答案 0 :(得分:1)

Thx @pat推进正确的方向。该问题似乎与以下

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

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

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

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

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

答案 1 :(得分:0)

您未在规范中的任何位置致电index(来自SphinxHelpers) - 因此,Sphinx永远不会更新您的新记录。

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