Ruby on rails 从Selenium更改为Capybara Webkit后,cache_classes/Spork出现问题
有一段时间,我使用Selenium/Spork/Rspec,cache_类为false,一切似乎都正常 在切换到webkit时,我开始出现与cache_类相关的错误(例如,Expected User、get User),因此我一直在与它斗争,试图将cache_类设置为true 但是,无论我做什么,最终都会出现以下错误:Ruby on rails 从Selenium更改为Capybara Webkit后,cache_classes/Spork出现问题,ruby-on-rails,testing,rspec,webkit,spork,Ruby On Rails,Testing,Rspec,Webkit,Spork,有一段时间,我使用Selenium/Spork/Rspec,cache_类为false,一切似乎都正常 在切换到webkit时,我开始出现与cache_类相关的错误(例如,Expected User、get User),因此我一直在与它斗争,试图将cache_类设置为true 但是,无论我做什么,最终都会出现以下错误: Capybara::Driver::Webkit::WebkitInvalidResponseError: Unable to load URL: http://127.
Capybara::Driver::Webkit::WebkitInvalidResponseError:
Unable to load URL: http://127.0.0.1:56398/login
我试过各种各样的东西。。。包括:
- ActiveSupport::Dependencies.clear在prefork和each\u run块中
- 代码如下:
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
require 'database_cleaner'
require 'factory_girl'
require 'authlogic/test_case'
require 'email_spec'
include Authlogic::TestCase
require File.expand_path("../../config/environment", __FILE__)
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
ApplicationController.skip_before_filter :activate_authlogic
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.include Capybara::DSL
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation, {:except => %w[ages coefficients weights1 weights2 weights3 weights4 weights5 weights6 weights7 weights8 etfs]}
DatabaseCleaner.clean
end
config.before(:each) do
DatabaseCleaner.start
Capybara.current_driver = :webkit if example.metadata[:js]
#Capybara.current_driver = :selenium if example.metadata[:js]
activate_authlogic
ActiveRecord::Base.instantiate_observers
end
config.after(:each) do
DatabaseCleaner.clean
Capybara.use_default_driver if example.metadata[:js]
end
Capybara.default_selector = :css
end
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
## SUPPORT METHODS ##
## (erased for clarity) ##
## ##
ActiveSupport::Dependencies.clear
end
Spork.each_run do
#FactoryGirl.reload
# Required to fix a recurring error when testing Active_Admin stuff
# See here: http://railsgotchas.wordpress.com/2012/01/31/activeadmin-spork-and-the-infamous-undefined-local-variable-or-method-view_factory/
# Delete at some point if active admin or whoever fixes this
ActionView::Template.register_template_handler :arb, lambda { |template|
"self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
}
#ActiveSupport::Dependencies.clear
end
更新:添加一个示例规范,以防万一
describe "Items" do
before(:each) do
@user = Factory.create(:user)
activate_authlogic
b = # something not important
end
describe "usage paths" do
it "the form directly from the basic_simulation show page should have correctly functioning javascript validation", :js => true do
request_sign_in(@user) # This is a helper method which goes through the login form
visit '/basic_simulation'
fill_in "amount", :with => "-5000"
click_button "Calculate"
page.should have_selector("label.jquery-validator.amount-error", :text => "Please enter a value greater than or")
fill_in "amount", :with => "5000"
click_button "Calculate"
page.should have_selector("input#amount", :value => "5000")
end
end
由于Capybara webkit和测试套件的线程问题,您遇到了问题 何塞·瓦利姆(Jose Valim)在书中更清楚地解释了这一点 如果您遵循他的建议,那么您应该能够打开事务性装置,完全删除数据库清理器,并且在使用capybara webkit的测试期间不再有数据问题。在测试性能时,您也会得到很好的提升 不过,诀窍是确保Jose的建议在
Spork中。每次运行块,否则它将不起作用。为了清楚起见,这里是myspec\u helper.rb
的相关部分
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
Capybara.default_driver = :rack_test
Capybara.default_selector = :css
Capybara.javascript_driver = :webkit
end
end
Spork.each_run do
if Spork.using_spork?
ActiveRecord::Base.instantiate_observers
end
require 'factory_girl_rails'
# Forces all threads to share the same connection, works on Capybara because it starts the web server in a thread.
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end
其他一些小建议:
- 如果您使用的是最新版本的factory_girl_rails,那么
应在
Spork中使用require factory\u girl\u rails
。每次运行
块和require-factory\u-girl
应从prefork
- 最新的factory\u girl\u rails也不再需要
ActiveSupport::Dependencies。请完全清除,尽管有些人在没有它的情况下仍然存在问题,因此您应该测试删除它
- 我仍然不确定是否需要
ActiveRecord::Base.instantiate_observators
,但在任何情况下,只有在您使用观察器时才需要它,而且我知道它应该在每个_run
块中
尝试所有这些,看看它是否适合您。您提到这允许我重新打开事务性装置-从非常快速的谷歌搜索看来,我的问题在于缓存类。。。上述方法是否允许我将两者(使用事务性工具和缓存类)都设置为true?当使用“Capybara.javascript\u driver=:webkit”时,您在测试中如何指示运行javascript?(我目前正在与Capybara.current\u driver=xx进行:js=>true业务,如果元数据…)是的。您可以在test.rb
文件中更改config.cache\u classes=true
。指示js文件与之前相同-这是一个Capabara指示器,它将传递给在spec\u helper.rb
中设置的选定js驱动程序。我尝试了一下,但是javascript仍然是一个问题。。。我得到以下eror:Capybara::Driver::Webkit::WebkitInvalidResponseError:无法加载URL:我得到的每个javascript规范的错误都完全相同,因为我让驱动程序通过登录页面登录。你知道这可能是什么原因吗?我有一个几乎相同的spec_helper.rb文件。。。我将在原始问题中发布一个示例规范