如何确保在我的Capybara测试中不为每个场景调用Redis和思考sphinx

时间:2023-02-15 20:47:23

I am using Capybara with Cucumber in my rails application. I want to run the Sphinx reindex and the Redis server to be running for some specific Test Scenarios. But the downside here is the scenarios are very very slow making it run for hours.

我在导轨应用程序中使用Capybara和Cucumber。我想运行Sphinx reindex和Redis服务器来运行某些特定的测试场景。但这里的缺点是场景非常缓慢,使其运行数小时。

Here is my env.rb file

这是我的env.rb文件

require 'rubygems'

ENV["RAILS_ENV"] = "cucumber"

require 'cucumber/rails'
require 'capybara/cucumber'
require 'capybara/rails'
require 'capybara/session'
require 'rake'

Capybara.default_selector = :css
ActionController::Base.allow_rescue = false
Cucumber::Rails::World.use_transactional_fixtures = true
Capybara.default_wait_time = 4
Capybara.ignore_hidden_elements=false
Capybara::Server.new(Capybara.app).boot

include Rake::DSL
Rake::Task["db:fixtures:load"].invoke

Before('@javascript') do
  Capybara.register_driver :selenium do |app|
    Capybara::Selenium::Driver.new(app,:browser=>:chrome )
  end
  Capybara.current_driver = :selenium
  Capybara.javascript_driver=:selenium
end

Before('@javascript', '@index') do
  `rake ts:in RAILS_ENV=cucumber` unless ThinkingSphinx.sphinx_running?
  `rake ts:start RAILS_ENV=cucumber`
end

After('@javascript','@index') do
  `rake ts:stop RAILS_ENV=cucumber`
end

Before('@javascript','@redis','@javascript') do
  `redis-server config/test_redis.conf`
  `rake redis_specifics:cache_build RAILS_ENV=cucumber --trace`
end

After('@javascript','@redis','@javascript') do
  pid = `ps aux | grep 'config/test_redis.conf'| grep -v 'grep' | awk '{print $2}'`
  `pkill #{pid}`
end

One more problem is when I run this on different machines, there is a dependency that Redis is installed and that the user is having permission to create and destroy folders. Is there a better way to handle this situation? May be to implement the similar transactional style fixtures that the Normal testing framework of Rails uses ?

还有一个问题是,当我在不同的机器上运行它时,存在一个安装Redis的依赖项,并且用户有权创建和销毁文件夹。有没有更好的方法来处理这种情况?可能是为了实现Rails的Normal测试框架使用的类似事务风格的fixture?

Any help is greatly appreciated.

任何帮助是极大的赞赏。

1 个解决方案

#1


1  

I'd suggest using a library like fakeredis (https://github.com/guilleiguaran/fakeredis). This will get rid of the dependency on redis in your tests, and ensure that data is not kept around between tests.

我建议使用像fakeredis这样的库(https://github.com/guilleiguaran/fakeredis)。这样可以消除测试中对redis的依赖,并确保测试之间不会保留数据。

I'd expect there to be something similar for sphinx, but don't know of anything off the top of my head.

我希望狮身人面像有类似的东西,但我不知道任何东西。

#1


1  

I'd suggest using a library like fakeredis (https://github.com/guilleiguaran/fakeredis). This will get rid of the dependency on redis in your tests, and ensure that data is not kept around between tests.

我建议使用像fakeredis这样的库(https://github.com/guilleiguaran/fakeredis)。这样可以消除测试中对redis的依赖,并确保测试之间不会保留数据。

I'd expect there to be something similar for sphinx, but don't know of anything off the top of my head.

我希望狮身人面像有类似的东西,但我不知道任何东西。