搜索功能在开发和生产中运行良好。但是当我运行 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 我刚刚发现如果我不清除索引和数据库,并且在运行两次测试后,我能够搜索到第一次运行测试中的结果。