【问题标题】:Rspec capybara spec fails when invoked via 'rake spec' but passes when invoked via 'rspec spec'Rspec capybara 规范在通过“rake spec”调用时失败,但在通过“rspec spec”调用时通过
【发布时间】:2012-11-28 17:15:19
【问题描述】:

我已经尝试过这些问题的答案:12。第一个表明它可能是一个缓存问题(并建议运行rake tmp:clear)。第二个建议在spec_helper.rb 中强制ENV['RAILS_ENV'] = 'test',因为rake 在“开发”中运行。我也尝试过完全移除 vcr gem,但这也没有效果。

comment 提供了有关问题可能是由于环境肮脏引起的更多详细信息(因为 rake 在“开发”中启动,但 rspec 在“测试”中运行)。

有问题的规范使用 capybara 和 capybara-webkit。它在通过bundle exec rake spec:requests 运行时通过,但在通过bundle exec rake 在套件中运行时失败。 capybara 看到的 html 在这两种情况下都不同,在套件中运行时看不到元素 #movie_imdb_id

我正在使用 gem 版本 rspec (2.11.0)capybara (1.1.3)capybara-webkit (0.12.1)

Rspec 消息:

Failures:

  1) Imdb credits get credits should have a credits table
     Failure/Error: wait_until { page.find('#movie_imdb_id').visible? }
     Capybara::ElementNotFound:
       Unable to find css "#movie_imdb_id"
     # (eval):2:in `find'
     # ./spec/requests/imdb_credits_spec.rb:9:in `block (3 levels) in <top (required)>'
     # ./spec/requests/imdb_credits_spec.rb:9:in `block (2 levels) in <top (required)>'

spec_helper.rb:

ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'database_cleaner'
require 'paper_trail'
require 'factory_girl'
require 'vcr'
require 'imdb_patch'
require 'capybara/rails'
# require 'capybara/rspec'
require 'capybara/webkit'

ENV['APP_DOMAIN'] = 'www.example.com'
ENV['DOMAIN'] = 'example.com'

# Require the fanatic and food factories
require Fanatic::Engine.config.root + 'spec/factories'
Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}

# 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}

RSpec.configure do |config|
  config.include Warden::Test::Helpers
  config.include PostsHelper
  config.include SpecHelpers

  Capybara.javascript_driver = :webkit

  # ## 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

  # 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

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    PaperTrail.enabled = false
  end

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

  config.after(:each) do
    Rails.cache.clear
    DatabaseCleaner.clean
    Warden.test_reset!
  end

  # 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.include FactoryGirl::Syntax::Methods
end

VCR.configure do |c|
  c.ignore_localhost = true
  c.hook_into :fakeweb
  c.cassette_library_dir = Rails.root.join('spec/fixtures/vcr_cassettes')
end

spec/requests/imdb_credits_spec.rb:

require 'spec_helper'

describe 'Imdb credits', :js => true do
  before(:each) do
    login_as_admin

    visit '/admin/movie/new'
    Rails.logger.debug page.html.to_yaml
    wait_until { page.find('#movie_imdb_id').visible? }

    fill_in 'Title',   :with => 'Batman'
    fill_in 'Imdb id', :with => '0096895'

    VCR.use_cassette('imdb_get', :match_requests_on => [get_matcher]) do
      click_button 'Fetch Imdb credits'
      wait_until { page.find('table#imdb_credits').visible? }
    end
  end

  describe 'get credits' do

    it 'should have a credits table' do
      page.should have_selector('table#imdb_credits')
    end

  end
end

【问题讨论】:

  • bundle exec rspec spec 工作吗?
  • 感谢本杰明的回复。是的。
  • @BenjaminTan 我仍然希望能够运行bundle exec rake。有什么想法吗?

标签: ruby-on-rails rspec capybara capybara-webkit


【解决方案1】:

问题实际上是 gem rails_admin 以及它如何与 rake 交互。通过将以下内容添加到spec_helper.rb 来修复它:

ENV['SKIP_RAILS_ADMIN_INITIALIZER'] = 'false'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多