Rspec 中的 solr 拒绝连接 [英] connection rejected from solr in Rspec
问题描述
我使用 sunspot-rails 进行搜索.这些是 Rspec 的样子:
I use the sunspot-rails for search. These is a Rspec looks like:
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
但它不起作用.我失败了:
But it doesn't work. I got a failure:
Errno::ECONNREFUSED:
Connection refused - connect(2)
但是如果我在命令行中手动输入 rake sunspot:solr:start RAILS_ENV=test
,然后运行规范,它就通过了.
But if I type rake sunspot:solr:start RAILS_ENV=test
by hand in command line, and then run the spec, it passes.
怎么了?在测试模式下,rake sunspot:solr:start RAILS_ENV=test
不是等同于 system("rake", "sunspot:solr:start")
吗?
What's wrong? Isn't rake sunspot:solr:start RAILS_ENV=test
equivalent to system("rake", "sunspot:solr:start")
in test mode?
(我试过 `system("rake", "sunspot:solr:start RAILS_EVN=test").相同.)
(I tried `system("rake", "sunspot:solr:start RAILS_EVN=test"). Same.)
推荐答案
您的 before(:all)
可能只是没有给 Solr 足够的启动时间.
Your before(:all)
probably just isn't giving Solr enough time to start.
也就是说,您可能需要仔细考虑您在这里要求规范验证的内容.您可以使用 Fakeweb 之类的库模拟对 Solr 的调用.
That said, you'll probably want to think hard about just what you're asking your specs to verify here. You can go a long way with mocking out calls to Solr with a library like Fakeweb.
Pivotal Labs 还有一个名为 sunspot_matchers 的库,可以捕获关于您搜索的更细粒度的断言'正在调用.
Pivotal Labs also has a library called sunspot_matchers that can capture more fine-grained assertions about the searches you're invoking.
如果您要针对 Solr 进行真正的集成规范,我建议您在工作时保持测试 Solr 运行.Foreman 之类的工具可以帮助管理您的 Solr 流程.我可能会使用如下所示的 Procfile
:
If you are going for real integration specs against Solr, I advise just keeping a test Solr running while you work. A tool like Foreman can help manage your Solr proceses. I might use a Procfile
like the following:
solr_dev: rake sunspot:solr:run RAILS_ENV=development
solr_test: rake sunspot:solr:run RAILS_ENV=test
(如果没有 RAILS_ENV 提供给 foreman start
,当然开发是默认环境)
(Development is, of course, the default environment if no RAILS_ENV is otherwise provided to foreman start
)
最后,如果您想在您的规范内启动 Solr,那么您已经走在正确的轨道上.只需在其中投入 sleep
并有足够的时间让 Solr 在您的规范开始运行之前完全启动.如果在系统负载过重时这会在您的规范套件中引入一些不可预测的故障,请不要感到惊讶.
Finally, if you want to start Solr within your specs, you're already on the right track. Just toss a sleep
in there with enough time to let Solr fully boot itself before your specs start running. Don't be surprised if that introduces a bit of unpredictable failure into your spec suite when the system is under load.
before :all do
`sunspot-solr start`
begin
Sunspot.remove_all!
rescue Errno::ECONNREFUSED
sleep 1 && retry
end
end
这篇关于Rspec 中的 solr 拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!