在Rails 4中使用RSpec + Capybara时出错

时间:2014-02-05 13:31:30

标签: ruby-on-rails testing rspec capybara rspec-rails

我有一个简单的rails应用程序,我试图介绍基本测试。我跟着这个url

我的申请结构如下:

enter image description here

我试图测试单页:称为主页面。

在spec / features / main_pages_spec.rb中,我有以下代码:

 require 'spec_helper'

feature "soadevise" do
  feature "Main Page" do
scenario "should have the content 'Main Page' "
    visit '/main_page/home'
  expect(page).to have_content('Main Page')
   end
end

我的spec / spec_helper.rb看起来像这样:

 # 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
   config.include Capybara::DSL
   # 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"
 end

当我在终端中运行此命令时:

  MRMIOMP0903:soadevise $ bundle exec rspec spec/features/main_pages_spec.rb

我收到以下错误:

 /Users/am/Desktop/x/xx/rails_projects/mysql_apps/soadevise/
  spec/features/main_pages_spec.rb:6:
  in `block (2 levels) in <top (required)>': undefined method `visit' for #  
  <Class:0x007fb7237377b0> (NoMethodError)

  from /Users/am/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core 2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval'

  from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass'

   from /Users/am.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe'

我的gemfile包含以下内容:

 #For testing
 group :development, :test do
   gem "rspec-rails", "~> 2.14.1"
 end

 group :test do 
  gem "selenium-webdriver"
  gem "capybara"
 end

我也提到了link。 有人可以建议我做错了吗?

1 个答案:

答案 0 :(得分:0)

您正在调用两次功能。它应该是这样的:

require 'spec_helper'

    feature "soadevise Main Page" do
      scenario "should have the content 'Main Page'" do
         visit '/main_page/home'
         expect(page).to have_content('Main Page')
       end
    end