【问题标题】:How can I setup capybara with rails 4如何使用 rails 4 设置 capybara
【发布时间】:2015-12-23 23:39:51
【问题描述】:

我有 capybara,当我运行我的精选测试时出现错误,它表示测试环境中的待定迁移,但是当运行其他类型的测试时一切都很好。我已经在测试环境中运行了所有迁移,我已经在测试和开发环境中运行了 rails。

这是我的宝石文件

group :development, :test do 
    .... other gems ....
    gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'database_cleaner'
    gem 'capybara', '~> 2.5.0'
    gem 'capybara-webkit', '~> 1.7.1'
    gem 'selenium-webdriver'
    gem 'poltergeist'
    gem 'launchy-rails', '~> 0.0.1'
end

这是我的测试

# spec/features/sign_in_spec.rb
require 'rails_helper'

feature 'Visitor signs up' do

  it "signs me in", :type => :feature do
    visit new_user_session_path
    puts "page:    #{page.html.inspect}"    
    save_and_open_page
  end
end

谢谢!

更新:

我只是试试这个命令:

bin/rake db:drop db:create db:migrate RAILS_ENV=test 

该命令一切正常。然后我运行我的服务器: rails s -e test

我的浏览器出现同样的错误,“ActiveRecord::PendingMigrationError” http://localhost:3000/

然后我运行迁移命令 bin/rake db:迁移 RAILS_ENV=test

但它引发了错误“ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: 关系“用户”已经存在”

当我运行功能测试时,同样的错误。

这是我的助手:https://gist.github.com/israelb/e2f4b10ba5f94e1e8df2

【问题讨论】:

  • 能否提供您的rails_helper.rb 和spec_helper.rb?
  • 运行建议的 bin/rake db:migrate RAILS_ENV=tests 会发生什么?
  • 你为什么还要在测试环境中运行rails s? O_o
  • 仅用于检查迁移是否运行良好

标签: ruby-on-rails capybara bdd


【解决方案1】:

可能存在某种迁移问题。试试rake db:test:prepare 看看是否有帮助。

【讨论】:

    【解决方案2】:

    我找到了解决方案,我的 config/enviroment/test.rb 文件有问题,我必须通过这个来更改:

    Rails.application.configure do
      # Settings specified here will take precedence over those in config/application.rb.
    
      # The test environment is used exclusively to run your application's
      # test suite. You never need to work with it otherwise. Remember that
      # your test database is "scratch space" for the test suite and is wiped
      # and recreated between test runs. Don't rely on the data there!
      config.cache_classes = true
    
      # Do not eager load code on boot. This avoids loading your whole application
      # just for the purpose of running a single test. If you are using a tool that
      # preloads Rails for running tests, you may have to set it to true.
      config.eager_load = false
    
      # Configure static file server for tests with Cache-Control for performance.
      config.serve_static_files   = true
      config.static_cache_control = 'public, max-age=3600'
    
      # Show full error reports and disable caching.
      config.consider_all_requests_local       = true
      config.action_controller.perform_caching = false
    
      # Raise exceptions instead of rendering exception templates.
      config.action_dispatch.show_exceptions = false
    
      # Disable request forgery protection in test environment.
      config.action_controller.allow_forgery_protection = false
    
      # Tell Action Mailer not to deliver emails to the real world.
      # The :test delivery method accumulates sent emails in the
      # ActionMailer::Base.deliveries array.
      config.action_mailer.delivery_method = :test
    
      # Randomize the order test cases are executed.
      config.active_support.test_order = :random
    
      # Print deprecation notices to the stderr.
      config.active_support.deprecation = :stderr
    
      # Raises error for missing translations
      # config.action_view.raise_on_missing_translations = true
    end
    

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多