簡體   English   中英

在教程中使用RSpec在Rails上測試ruby時出錯

[英]Error in testing ruby on rails using rspec in tutorial

我是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

<h1>Sample App</h1>
<p>
 This is the home page for the
 <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
 sample application.
 </p>

並運行命令

$ bundle exec rspec spec/requests/static_pages_spec.rb

它給出了錯誤

c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:42:in `block in <top (
      required)>': 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 `<t
      op (required)>'
              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 `<top (required)>'
              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中的說明安裝了水豚,然后在sample_app / spec / spec_helper.rb中要求安裝'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 `<to
      p (required)>'
              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 `<top (required)>'
              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'

我的gemfile是

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以在開發/測試環境中包括水豚:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM