使用rspec测试时,太阳黑子搜索结果为空

时间:2013-08-27 23:44:02

标签: ruby-on-rails rspec sunspot

搜索功能在开发和生产中运行良好。但是当我运行Rspec测试时。结果数组总是空的

在我的搜索操作日志中,我可以看到数据已被索引并正确获取:

Parameters: {"utf8"=>"✓", "ga_client_id"=>"", "search"=>"David"}
SOLR Request (3.9ms)  [ path=#<RSolr::Client:0x007fb7f9141f00> parameters={data:       fq=type%3AToon&sort=created_at_d+asc&q=David&fl=%2A+score&qf=name_text+author_text+toon_typetext+tags_text&defType=dismax&start=0&rows=9, method: post, params: {:wt=>:ruby}, query: wt=ruby, headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"}, path: select, uri: http://localhost:8981/solr/select?wt=ruby, open_timeout: , read_timeout: , retry_503: , retry_after_limit: } ]
Toon Load (0.4ms)  SELECT "toons".* FROM "toons" WHERE "toons"."id" IN (15550, 15551, 15552) AND (state in ('created','flagged'))

在我的控制器中

  @search = Toon.solr_search do
    fulltext params[:search]
    order_by :created_at, :asc
    paginate :page => params[:page], :per_page => 9
  end
  @toons = @search.results

我的rspec测试:https://gist.github.com/jianbo/6360486

在测试视图

  <%= @search.inspect %> This output correctly as follow:
  <Sunspot::Search:{:fq=>["type:Toon"], :sort=>"created_at_d asc", :q=>"papabear", :fl=>"* score", :qf=>"name_text author_text toon_type_text tags_text", :defType=>"dismax", :start=>0, :rows=>9}>

  <%= @toons.inspect %> is empty array

UPDATE 我刚刚发现,如果我没有清除索引和数据库,并且在运行测试两次后,我就能够搜索第一次运行测试中的结果。

1 个答案:

答案 0 :(得分:-1)

您需要为测试启动Solr实例。这里有一个非常有用的代码段可以提供帮助:https://github.com/sunspot/sunspot/wiki/RSpec-and-Sunspot#running-sunspot-during-testing