【问题标题】:Undefined method 'visit' for #<Class:XYZ> (NoMethodError) [rspec]#<Class:XYZ> (NoMethodError) [rspec] 的未定义方法“访问”
【发布时间】:2012-08-11 02:33:28
【问题描述】:

我在尝试运行 rspec 测试时收到以下错误:

/srv/offerme/spec/requests/static_pages_spec.rb:13: undefined method `visit' for #<Class:0xb3436684> (NoMethodError)
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:12
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:3
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `autorun'
from /usr/local/bin/rspec:19

我收到此错误的文件如下所示 (spec/requests/static_pages_spec.rb):

require 'spec_helper'

describe "StaticPages" do
include Capybara::DSL
  describe "GET /static_pages" do
    it "works! (now write some real specs)" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get static_pages_index_path
      response.status.should be(200)
    end 
  end 
  describe "Home page" do
    visit 'static_pages/home'
    page.should have_content('OfferMe')
  end 

end

我的 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'
require 'capybara/rspec'

# 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}

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
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end

最后,我的 Gemfile 看起来像这样:

source 'https://rubygems.org'

gem 'rails', '3.2.7'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
  gem 'sqlite3'
  gem 'rspec-rails'
end

group :test do
  gem 'capybara'
end

gem 'json'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.4'
  gem 'coffee-rails', '~> 3.2.2'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
  gem 'twitter-bootstrap-rails'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

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

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug'

gem 'therubyracer', '0.10.1'
gem 'libv8'
gem 'webrat'

我已经看到了这个 GitHub issue 并尝试了一些解决方法,并查看了与类似问题有关的其他一些 StackOverflow 问题(这就是为什么一些代码看起来冗余/hacky)。但是,它们都还没有起作用。请帮忙!

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara


    【解决方案1】:

    更新:

    问题是您在it 块之外调用visit,这里:

    describe "Home page" do
      visit 'static_pages/home'
      page.should have_content('OfferMe')
    end
    

    将这些中间行包裹在 it 块中:

    describe "Home page" do
      it "has a homepage" do
        visit 'static_pages/home'
        page.should have_content('OfferMe')
      end
    end
    

    应该可以的。

    原始答案:

    我可能是错的,但我认为你必须在 Gemfile 的测试和开发部分都包含 capybara。

    尝试将 Gemfile 的该部分更改为:

    group :development, :test do
      gem 'sqlite3'
      gem 'rspec-rails'
      gem 'capybara'
    end
    

    【讨论】:

    • 嗯...好吧,我注意到的另一件事是您不需要在spec_helper.rb 中使用“capybara/rails”。在capybara github page 上,它指定如果您将 capybara 用于 rails 项目,您应该拥有它。尝试添加它,看看它是否有效。
    • 那也没用。 require 语句的顺序重要吗?
    • 嗯...不,那应该没关系。我没有看到任何其他可能导致它的原因。
    • 哦,等等,我明白了。您在 it 块之外有 visit,这是行不通的。您需要将其包装在 it 块中。
    • 好眼光!感谢您的帮助!
    【解决方案2】:

    这是一个旧帖子。我有同样的问题。我想我分享我的解决方案。 在我的 Gemfile 中,我有

    group :test do
      gem 'capybara'
    end
    

    visit 位于正确的位置,但我遇到了同样的错误。所以这就是我所做的。我在 Gem 上运行了更新,现在一切正常。

    bundle update capybara

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-05-25
      • 2014-09-26
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 2016-03-13
      • 2018-04-20
      相关资源
      最近更新 更多