在教程中使用rspec在rails上测试ruby时出错

我是ruby on rails的新手我只是在阅读http://ruby.railstutorial.org/chapters/static-pages#top作为教程说,我创建了sample_app包或项目。 现在在3.2.1节我正在测试应用程序。 首先,我从cmd运行以下命令

$ rails generate integration_test static_pages 

这在spec / requests目录中创建了static_pages_spec.rb。 现在在spec / requests / static_pages_spec.rb中添加以下代码

  require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end end end 

在这个阶段,我在sample_app / spec / spec_helper.rb文件中添加以下代码

 config.include Capybara::DSL 

所以我的文件看起来像

 # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' # 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 } # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) 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 # 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 = true # 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 # Run specs in random order to surface order dependencies. If you find an # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 config.order = "random" config.include Capybara::DSL end 

我有app / views / static_pages / home.html.erb

 

Sample App

This is the home page for the Ruby on Rails Tutorial sample application.

并运行该命令

 $ bundle exec rspec spec/requests/static_pages_spec.rb 

它给出了错误

 c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:42:in `block in ': uninitialized constant Capybara (NameError) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core.rb:120:in `configure' from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:15:in `' from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp ec.rb:1:in `require' from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp ec.rb:1:in `' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `load' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `each' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/command_line.rb:22:in `run' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/runner.rb:80:in `run' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/runner.rb:17:in `block in autorun' 

我在git步骤1-> gem install capybara步骤2->在sample_app / spec / spec_helper.rb中要求’capybara / rails’按照方向安装了水豚但是它仍然显示了capybara rails的加载错误现在我运行了相同的命令即$ bundle exec rspec spec / requests / static_pages_spec.rb

我收到以下错误

 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.0/lib/act ive_support/dependencies.rb:228:in `require': cannot load such file -- capybara/ rspec (LoadError) from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport- 4.0.0/lib/active_support/dependencies.rb:228:in `block in require' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport- 4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport- 4.0.0/lib/active_support/dependencies.rb:228:in `require' from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:8:in `' from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp ec.rb:1:in `require' from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp ec.rb:1:in `' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `load' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `each' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/command_line.rb:22:in `run' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/runner.rb:80:in `run' from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1 4.5/lib/rspec/core/runner.rb:17:in `block in autorun' 

我的gem文件是

 source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.0' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 1.2' group :doc do # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', require: false end # Use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' # Use unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano', group: :development # Use debugger # gem 'debugger', group: [:development, :test] group :development, :test do gem 'rspec-rails', ">= 2.0.0.beta" end gem 'rubyzip' 

如果我删除config.include Capybara :: DSL然后我得到失败错误

请给我正确的方向……先谢谢

既然你正在使用Bundler(即你有一个Gemfile ),你需要确保Gemfile也在那里,否则你的rails应用程序将不知道gem已安装。 更新您的Gemfile以在开发/测试环境中包含Gemfile

 group :development, :test do gem 'rspec-rails', ">= 2.0.0.beta" gem 'capybara' end