ruby-on-rails – 为什么RSpec功能规范使用Capybara,Poltergeist和Javascript失败不一致?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 为什么RSpec功能规范使用Capybara,Poltergeist和Javascript失败不一致?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨其他软件开发者,

我有一个Rails应用程序,通常设置testuite(RSpec,Capybara,Poltergeist,jQuery,Rails).我使用一些Javascript(jQuery)进行远程请求.基本上它工作,我还不明白我的问题.所以我可以用进一步的知识更新问题.

也许你可以在我发布许多不相关的代码和配置之前给我一些提示.

不一致和意外的行为在一个功能规范中,通过Capybara中的Poltergeist进行测试,用户交互(点击表单和链接)通过AJAX和jQuery隐藏和显示HTML元素.

如果我手动测试它似乎甚至可以正常工作.设置Capybara默认等待时间只改变了我必须等到spec run =)的时间.我发现没有其他相关配置或用法.

所以任何帮助/想法都非常感谢.提前致谢.

我的spec_helper:

require 'simplecov'
require 'rubygems'

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment",__FILE__)
require 'email_spec'
require 'rspec/autorun'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'capybara/rails'

Capybara.javascript_driver = :poltergeist

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  config.include(EmailSpec::Helpers)
  config.include(EmailSpec::Matchers)
  config.include Capybara::DSL
  #config.include Capybara::RSpecMatchers
  config.mock_with :rspec

  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.
  # set to false with Capybara + Database Cleaner
  # set to true with ActiveRecord::Base patch from Jose Valim and without Database Cleaner
  config.use_transactional_fixtures = false
  config.infer_base_class_for_anonymous_controllers = true
  config.order = "random"
  config.include FactoryGirl::Syntax::Methods

  config.after(:each) do
    # some model deletion
    model.delete_all
  end

  config.include FeatureHelpers
end

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @shared_connection || retrieve_connection
  end
end

# # Forces all threads to share the same connection. This works on Capybara
# # because it starts the web server in a thread
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

FactoryGirl.reload

解决方法

对于rails应用程序,这是一个全新的capybara-poltergeist设置,你能检查一下这个和你的版本之间的区别吗? (我太懒了^ _ ^)

的Gemfile

group :development,:test do
  gem 'rspec-rails'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'poltergeist'
  gem 'phantomjs',require: 'phantomjs/poltergeist'
end

投机/ spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment",__FILE__)
require 'rspec/rails'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'

Capybara.javascript_driver = :poltergeist

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.order = "random"
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = false
  config.infer_base_class_for_anonymous_controllers = false
end

规格/支持/ database_cleaner.rb

require "database_cleaner"

RSpec.configure do |config|
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each,:js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

规格/功能/ posts_spec.rb

require "spec_helper"

describe "Posts pages" do
  let!(:post) { create :post } # Factory girl

  it "has post",js: true do
    visit posts_path
    expect(page).to have_content post.title
  end
end

猜你在找的Ruby相关文章