2012-08-11 75 views
3

我收到以下錯誤,當我嘗試運行一個RSpec測試:未定義的方法「參觀」爲#<Class:XYZ>(NoMethodError)的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 

的文件在我看起來像這樣得到這個錯誤(規格/請求/ 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的問題(這就是爲什麼一些該代碼看起來多餘/哈克)。但是,他們都沒有工作。請幫忙!

回答

6

UPDATE:

的問題是,我們在調用visit外的it塊,在這裏:

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的測試和開發的部分水豚。

試着改變你的Gemfile的那款這樣的:

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

沒有工作:( – Ridwan 2012-08-11 02:50:16

+1

嗯......好吧,我注意到的另外一件事是你不需要'spec_helper.rb'中的'capybara/rails'。在[capybara github頁面](https ://github.com/jnicklas/capybara/)它指定你應該有這個,如果你使用capybara作爲一個rails項目,試着添加它並看看它是否有效 – 2012-08-11 02:57:27

+0

這也行不通。要求聲明很重要嗎? – Ridwan 2012-08-11 03:01:26

0

這是一個老帖子。我有同樣的問題。我想我分享我的解決方案。 在我的Gemfile我有

group :test do 
    gem 'capybara' 
end 

而且visit是在正確的位置,但是我居然也得到了同樣的錯誤。所以這就是我所做的。我在Gem上運行了更新,現在都很好。

bundle update capybara

希望這有助於別人。

相關問題