为什么我在Rails 4.1中使用带有Capybara 2.3的RSpec 3.0时出错?

时间:2014-06-12 18:42:56

标签: ruby-on-rails rspec capybara

我是Rails开发的新手。我正在关注Michael Hartl的视频教程。但是我无法使用Capybara / RSpec运行测试。我收到了一些错误。我的设置如下:

我使用的是Ruby 2.0.0p353。 Rails - 4.1.1和 RSpec - 3.0.1 Capybara - 2.3.0

以下是我的 GemFile。

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
ruby '2.0.0'
gem 'rails', '4.1.1'
gem 'bootstrap-sass'
gem 'bcrypt'
gem 'faker'
gem 'will_paginate'
gem 'bootstrap-will_paginate'
gem 'rails_12factor'

# Use sqlite3 as the database for Active Record
group :development, :test do
   gem 'sqlite3'
   gem 'rspec-rails'
   gem 'guard-rspec'
   gem 'guard-spork'
   gem 'childprocess'
   gem 'spork'
end

#Gems used only for assets and not used in production enviornment by default
group :assets do
   # Use SCSS for stylesheets
   gem 'sass-rails', '~> 4.0.3'
   # 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'
end


group :test do
   gem 'capybara'
   gem 'factory_girl_rails'
   gem 'cucumber-rails'
   gem 'database_cleaner'
   #gem 'launchy'
   #gem 'rb-fsevent'
   #gem 'growl'
end

group :production do
   gem 'pg'
end


# 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', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

以下是 spec / spec_helper.rb:

    require 'capybara/rspec'

    RSpec.configure do |config|

       config.include Capybara::DSL
    end

而且,这是我的规范。 test - spec / features / 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'
     page.should have_content('Sample App')
      end

   end

end

如果我运行命令 rspec / featues / static_pages_spec.rb ,我会得到一个很长的堆栈跟踪。它显示了两个主要问题:

/usr/local/share/ruby/site_ruby/rubygems/core_ext/kernel_require.rb:73:
warning: loading in progress, **circular require considered harmful - 

/home/MAC/.gem/ruby/gems/capybara-2.3.0/lib/capybara.rb**

  An error occurred in an after hook
    **ArgumentError: rack-test requires a rack application, but none was given**
    occurred at /home/MAC/.gem/ruby/gems/capybara-2.3.0/lib/capybara/rack_test/driver.rb:16:in `initialize'

F

Failures:

  1) Static pages Home page should have the content  'Sample App'
     Failure/Error: visit '/static_pages/home'
     ArgumentError:
       **rack-test requires a rack application, but none was given**
     # ./spec/features/static_pages_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.00062 seconds (files took 0.58874 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/static_pages_spec.rb:8 # Static pages Home page should have the content  'Sample App'

我试过谷歌搜索这个问题。似乎Capybara 2.3.0和RSpec 3.0.1不能很好地发挥。但是我没有找到解决问题的任何解决方案。有没有人遇到并解决过这样的问题?请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

rspec-rails 3不再默认根据位置设置规格类型,因此您的规范不会被设置为功能规范

您可以单独标记要素规范,也可以添加

config.infer_spec_type_from_file_location!

要使用规范助手恢复旧行为