简体   繁体   中英

Undefined method 'visit' (using rspec and capybara)

Upon trying to run a specs files for integration testing, I see:

undefined method `visit' for #<Class:0xab9a140> (NoMethodError)

I've searched this website and found out several solutions to my problems, and these are the solutions I've tried already ( with no success ):

  • added file under spec/features instead of spec/requests (they have always been there)
  • add include Capybara::DSL to my spec file - this is funny. When I add this, I stop having the aforementioned error and start having another error message :

    • undefined method 'expect' for #<Class:0x9d2a088> (NoMethodError)
  • replacing describe with feature in the spec tests. (no change)

Outcome of trying to run the specs which contain the visit method:

felipe@felipe-VirtualBox:~/rails/project-manager$ bundle exec rspec spec/features/
No DRb server is running. Running in local process instead ...
/home/felipe/rails/project-manager/spec/features/projects_spec.rb:15:in `block (2 levels) in <top (required)>': undefined method `visit' for #<Class:0xab9a140> (NoMethodError)
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe'
from /home/felipe/rails/project-manager/spec/features/projects_spec.rb:14:in `block in <top (required)>'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `module_eval'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:246:in `subclass'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/example_group.rb:232:in `describe'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/dsl.rb:18:in `describe'
from /home/felipe/rails/project-manager/spec/features/projects_spec.rb:3:in `<top (required)>'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:77:in `rescue in run'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:73:in `run'
from /home/felipe/.rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'

my Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'

group :development,:test do
  gem 'rspec-rails'
  gem 'pg','0.15.1'
  gem 'watchr'
  gem 'selenium-webdriver'
  gem 'capybara'
  gem 'spork-rails'
  gem 'guard-spork'
  gem 'childprocess'
end

gem 'sass-rails', '~> 4.0.1'

gem 'uglifier', '>= 2.1.0'

gem 'coffee-rails', '~> 4.0.1'

gem 'jquery-rails','3.0.4'

gem 'turbolinks','1.1.1'

gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

My spec_helper.rb file

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

  RSpec.configure do |config|

    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    config.use_transactional_fixtures = true

    config.infer_base_class_for_anonymous_controllers = false

    config.order = "random"
    # Disable the old-style object.should syntax.
    config.expect_with :rspec do |c|
       c.syntax = :expect
    end
  end
end

Spork.each_run do

end

the actual test (yes, it's under spec/features folder)

require 'spec_helper'

describe "Projects" do
  describe "Projects index" do

    it "should show the string 'Projetos'" do

      visit '/projects'
      expect(page).to have_content('Projetos')

    end
  end

  describe 'Add and delete a project' do

    visit '/projects/new'

    expect(page).to have_content('Novo')

  end 

end

I'm looking for suggestions and pointers as to what might be my issue.

PS: I'm running rails (4.0.1) , capybara (2.2.0) , rspec-core (2.14.7) rspec-rails (2.14.0)

我发现这个问题....我错过了it ... do ... end围绕第二次调用块visit ......对不起,这并感谢大家谁帮助我。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM