连接在Rspec中被solr拒绝

Lai*_*uan 9 solr ruby-on-rails sunspot

我用sunspot-rails进行搜索.这些是Rspec看起来像:

describe "GET search" do
  before(:all) do
    system("rake", "sunspot:solr:start")
  end

  after(:all) do
    system("rake", "sunspot:solr:stop")
  end

  it "should do some search" do
    Text.search do
      ...
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我失败了:

Errno::ECONNREFUSED:
   Connection refused - connect(2)
Run Code Online (Sandbox Code Playgroud)

但是如果我rake sunspot:solr:start RAILS_ENV=test在命令行中手动输入,然后运行规范,它就会通过.

怎么了?是不是rake sunspot:solr:start RAILS_ENV=test相当于system("rake", "sunspot:solr:start")在测试模式?

(我试过`system("rake","sunspot:solr:start RAILS_EVN = test").相同.)

Nic*_*zny 14

before(:all)可能只是没有给索尔足够的时间开始.

也就是说,您可能想要仔细考虑您要求您的规格在此验证的内容.你可以通过像Fakeweb这样的库来模拟对Solr的调用.

Pivotal Labs还有一个名为sunspot_matchers的库,可以捕获有关您正在调用的搜索的更细粒度的断言.

如果您要针对Solr进行真正的集成规范,我建议您在工作时保持测试Solr运行.像Foreman这样的工具可以帮助管理您的Solr程序.我可能会使用Procfile以下内容:

solr_dev:  rake sunspot:solr:run RAILS_ENV=development
solr_test: rake sunspot:solr:run RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)

(当然,如果没有提供RAILS_ENV,则开发是默认环境foreman start)

最后,如果您想在规范中启动Solr,那么您已经走在了正确的轨道上.只需sleep在那里投入足够的时间让Solr在您的规格开始运行之前完全启动.如果在系统负载不足时将一些不可预测的故障引入规范套件,请不要感到惊讶.

[编辑:快速和肮脏的before :all,它使用Sunspot.remove_all轮询的可用性.]

before :all do
  `sunspot-solr start`
  begin
    Sunspot.remove_all!
  rescue Errno::ECONNREFUSED
    sleep 1 && retry
  end
end
Run Code Online (Sandbox Code Playgroud)


Sim*_*mmo 11

sunspot_test宝石会为你做到这一点,并且支持RSpec的.