Rspec 中 solr 拒绝连接
我使用 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
但它不起作用。我失败了:
Errno::ECONNREFUSED:
Connection refused - connect(2)
但是如果我在命令行中手动输入 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")。相同。)
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)
But if I type rake sunspot:solr:start RAILS_ENV=test
by hand in command line, and then run the spec, it passes.
What's wrong? Isn't rake sunspot:solr:start RAILS_ENV=test
equivalent to system("rake", "sunspot:solr:start")
in test mode?
(I tried `system("rake", "sunspot:solr:start RAILS_EVN=test"). Same.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
before(:all)
可能只是没有给 Solr 足够的时间来启动。也就是说,您可能需要认真考虑一下您在这里要求规格验证的内容。您可以使用像 Fakeweb 这样的库来模拟对 Solr 的调用。
Pivotal Labs 还有一个名为 sunspot_matchers 的库,它可以捕获有关您正在调用的搜索的更细粒度的断言。
如果您想要针对 Solr 进行真正的集成规范,我建议您在工作时保持测试 Solr 运行。像 Foreman 这样的工具可以帮助管理您的 Solr 进程。我可能会使用如下的
Procfile
:(当然,如果没有向
foreman start
提供 RAILS_ENV,则开发是默认环境)最后,如果您想开始Solr 在您的规格范围内,您已经走在正确的轨道上。只需在其中进行一个睡眠,留出足够的时间让 Solr 在您的规范开始运行之前完全启动。当系统处于负载状态时,如果这会给您的规格套件带来一些不可预测的故障,请不要感到惊讶。
[编辑:
before :all
使用Sunspot.remove_all
轮询可用性。]Your
before(:all)
probably just isn't giving Solr enough time to start.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 also has a library called sunspot_matchers that can capture more fine-grained assertions about the searches you're invoking.
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:(Development is, of course, the default environment if no RAILS_ENV is otherwise provided to
foreman start
)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.[Edit: Quick and dirty
before :all
which usesSunspot.remove_all
to poll for availability.]sunspot_test gem 将为您执行此操作并支持 RSpec。
The sunspot_test gem will do this for you and supports RSpec.
这是一个疯狂的猜测,但我敢打赌,您在 config/environments/development.rb 文件中配置了一个 Solr 服务器,以便在给定端口上本地查看,但您的 中没有此类配置>config/environments/test.rb
这会导致它连接到默认地址/端口,在执行测试时,您实际上并没有在该地址/端口上运行 Solr 服务器。
我对 Ruby 中的 Solr 客户端了解不够,无法确定这一点,但由于还没有其他人参与其中,我希望这能为您指明正确的方向。
This is a wild-ass guess, but I bet you have a Solr server configured in your config/environments/development.rb file to look locally on a given port, but no such configuration in your config/environments/test.rb
This is causing it to connect to the default address/port where you do not in fact have a Solr server running when you execute your tests.
I don't know enough about the Solr client in Ruby to be sure of this, but since no one else has weighed in yet I hope this points you in the right direction.
我只需添加
到spec_helper.rb
即可实现此功能编辑:我最终选择了 https://github.com/collectiveidea /sunspot_test 就像 Simmo 提到的。由于某种原因,它在每次测试运行时都重新运行 rake 任务(即使我将它放在 spork 的 prefork 块中)。不知道为什么,但 sunspot_test gem 似乎是目前的最佳选择。
I got this working just by adding
to spec_helper.rb
Edit: I ended up going with https://github.com/collectiveidea/sunspot_test like Simmo mentioned. It was re-running the rake task on every test run for some reason (even though I had it in the prefork block of spork). Not sure why, but the sunspot_test gem seems like the way to go for now.