sunspot 和 rspec 测试失败,因为 solr 没有找到任何结果

sunspot and rspec test failing as solr doesn't find any results

我正在测试 Rspec 我的 sunspot solr 配置。

我有:

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

  describe "GET index" do
    it "assigns all companies as @companies" do
      get :index, {}, valid_session
      expect(assigns(:companies)).to eq([@company])
    end
  end

和我的索引控制器:

  def index
    @search = Company.search do
      fulltext params[:search] 
    end
    @companies = @search.results
  end

测试失败:

 Failure/Error: expect(assigns(:companies)).to eq([@company])

   expected: [#<Company id: 613, name: "company 1", description: "description company 1", size: nil, company_link: "www.company1.com", created_at: "2015-04-13 12:52:56", updated_at: "2015-04-13 12:52:56">]
        got: []

有什么线索吗?

添加到我的 factory_girl 工厂

 after(:create) { Company.reindex }

解决了这个问题,Solr 现在会在每次创建后重新索引所有内容,以便索引始终保持一致。

您很可能有 RSpec 的 config.use_transactional_fixtures = true。在这种情况下,您可以将以下内容添加到要索引的工厂中:

after(:create) { |c| c.index! }

参考:https://github.com/sunspot/sunspot/issues/182