无法在Rails中设置capybara

时间:2014-06-03 09:35:00

标签: ruby-on-rails rspec capybara

我正在学习如何使用Capybara在Rails中的简单应用上进行集成测试。

但是我在运行&rs; rspec规范时遇到了错误:

bundle exec rspec spec
/home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/dsl.rb:1: warning: loading in progress, circular require considered harmful - /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara.rb
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rspec:23:in `<main>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rspec:23:in `load'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:38:in `invoke'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:70:in `run'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:85:in `run'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:96:in `setup'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:22:in `configure'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:100:in `process_options_into'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:100:in `each'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:101:in `block in process_options_into'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `requires='
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `each'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `block in requires='
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `require'
    from /home/alex/git-projects/rails-projects/qotd2/spec/spec_helper.rb:6:in `<top (required)>'
    from /home/alex/git-projects/rails-projects/qotd2/spec/spec_helper.rb:6:in `require'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:1:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:1:in `require'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara.rb:5:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara.rb:320:in `<module:Capybara>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara.rb:320:in `require'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/dsl.rb:1:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/dsl.rb:1:in `require'
/home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/node/matchers.rb:121: warning: assigned but unused variable - e
/home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/selenium/driver.rb:119: warning: assigned but unused variable - a
/home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:6:in `block (2 levels) in <top (required)>': uninitialized constant Rails (NameError)
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:145:in `new'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:145:in `block in generate_map'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:145:in `each'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:145:in `generate_map'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rack-1.5.2/lib/rack/builder.rb:132:in `to_app'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/capybara-2.3.0/lib/capybara/rails.rb:13:in `<top (required)>'
    from /home/alex/git-projects/rails-projects/qotd2/spec/spec_helper.rb:6:in `require'
    from /home/alex/git-projects/rails-projects/qotd2/spec/spec_helper.rb:6:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `require'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `block in requires='
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `each'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1018:in `requires='
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:101:in `block in process_options_into'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:100:in `each'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:100:in `process_options_into'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/configuration_options.rb:22:in `configure'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:96:in `setup'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:85:in `run'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:70:in `run'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:38:in `invoke'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rspec:23:in `load'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/rspec:23:in `<main>'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
    from /home/alex/.rvm/gems/ruby-2.0.0-p451@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'

我的Gemfile(部分):

group :test do
    gem 'fake'
    gem 'capybara'
    gem 'database_cleaner'
    gem 'launchy'
end

group :development, :test do
    gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'guard-rspec'
    gem 'growl'
    gem 'dotenv-rails'
end

我的测试用例(location: /spec/features/users_spec.rb)

require 'spec_helper'

describe 'User authentication' do 
    context 'new user signs up' do
        it "saves the new user" do 
            visit "/"
            expect {
                click_button 'Sign up'
                fill_in 'Name', with: 'alexjiao'
                fill_in 'Email', with: "alexjiao@yahoo.com"
                fill_in 'Password', with: 'asdfasdf'
                fill_in 'Password confirmation', with: 'asdfasdf'
                click_button 'Create my account'
            }.to change(User, :count).by(1)
        end
    end

end

我的spec_helper.rb (location: /spec/spec_helper.rb)

require 'capybara/rails'
require 'capybara/rspec'

RSpec.configure do |config|
  config.include Capybara::DSL
end

一些解决方案,例如将测试用例放入feature文件夹并为capybara DSL创建配置语句都在这里。但我似乎无法让水豚工作。似乎我的情况有点独特,尤其是上面的错误消息。需要注意的一点是,在运行rails g rspec:install后,我的spec_helper.rb为空(这是正常的吗?)。因此,这说明了上面的简洁代码。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我有类似的问题。仅使用

require 'capybara'

在我的spec_helper.rb文件中,而不是

require 'capybara/rails'
require 'capybara/rspec'

超越了“未初始化的常量Rails(NameError)”。

我仍然收到有关循环要求的警告,但测试运行正确。

*编辑*
我不得不补充道:

--require spec_helper
--require rails_helper

在项目目录中的.rspec文件中,以使测试最终正确运行(RSpec 3.0)。

答案 2 :(得分:0)

@ mkmadd的回答解决了我的问题。我还建议遵循Romanwv的提示并从.rspec删除--warnings以避免一些麻烦。