undefined method `visit' for #<RSpec::Core::ExampleGroup:.... #360

Closed
be9 opened this Issue May 2, 2011 · 65 comments

Comments

Projects
None yet
@be9
Contributor

be9 commented May 2, 2011

I'm having trouble with my Capybara integration specs and rspec-rails 2.6.0.rc4.

  9) My profile should show profile
     Failure/Error: login_as @user
     undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_4:0x748aab0>
     # ./spec/support/request_helpers.rb:8:in `login_as'
     # ./spec/integration/user/profile_spec.rb:6

It's simple visit '/' which explodes.

2.6.0.rc2 works great. What has been changed? Capybara version is 1.0.0.beta1

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky May 2, 2011

Member

rc4 adds more support for Capybara 0.4 and 1.0. How/where are you requiring capybara?

Member

dchelimsky commented May 2, 2011

rc4 adds more support for Capybara 0.4 and 1.0. How/where are you requiring capybara?

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 2, 2011

Contributor

I have require 'capybara/rspec' line in spec/support/additional_requires.rb

Contributor

be9 commented May 2, 2011

I have require 'capybara/rspec' line in spec/support/additional_requires.rb

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky May 2, 2011

Member

Try getting rid of that and tell me what happens. rspec-rails already does this, so it might be that doing it a second time is causing a problem due to module reloading, etc.

Member

dchelimsky commented May 2, 2011

Try getting rid of that and tell me what happens. rspec-rails already does this, so it might be that doing it a second time is causing a problem due to module reloading, etc.

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 2, 2011

Contributor

I've commented out the require line, but nothing changed. visit is still broken.

Contributor

be9 commented May 2, 2011

I've commented out the require line, but nothing changed. visit is still broken.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky May 2, 2011

Member

What's in your Gemfile?

Member

dchelimsky commented May 2, 2011

What's in your Gemfile?

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 2, 2011

Contributor

Posted Gemfile here: https://gist.github.com/952141

Contributor

be9 commented May 2, 2011

Posted Gemfile here: https://gist.github.com/952141

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky May 3, 2011

Member

I just ran this: https://gist.github.com/952630 and the spec with visit in it passed. Do you get the same result from that gist?

Member

dchelimsky commented May 3, 2011

I just ran this: https://gist.github.com/952630 and the spec with visit in it passed. Do you get the same result from that gist?

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 3, 2011

Contributor

Works here too. I tried to make visit explode by copying in Gemfile and other stuff from my project but it still passes.

On the other side, I experimented with rspec-rails git and found out that this commit exactly: 59793dc breaks things for me (the specs pass with the preceding one when rspec-rails is pulled from git).

Contributor

be9 commented May 3, 2011

Works here too. I tried to make visit explode by copying in Gemfile and other stuff from my project but it still passes.

On the other side, I experimented with rspec-rails git and found out that this commit exactly: 59793dc breaks things for me (the specs pass with the preceding one when rspec-rails is pulled from git).

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 3, 2011

Contributor

The test example works if I manually add include Capybara::DSL.

How come it doesn't get included?

Contributor

be9 commented May 3, 2011

The test example works if I manually add include Capybara::DSL.

How come it doesn't get included?

@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 3, 2011

Contributor

So, I found it. The example that also breaks for your example app:

require "spec_helper"
describe "things" do
    it "works with visit" do
      go_things
      page.status_code.should eq(200)
    end
end

The cause of the bug is as follows. If we look at the :include_or_extend_modules array in rspec configuration, we see following:

   [[:extend, RSpec::Matchers::DSL, {}],
     [:include, RSpec::Rails::FixtureSupport, {}],
     [:include, RSpec::Rails::Mocks, {}],
     [:include, Capybara::Features, {:capybara_feature=>true}],
     [:include, Capybara::DSL, {:type=>:request}],
     [:include, Capybara::DSL, {:type=>:acceptance}],
     [:include, Capybara::RSpecMatchers, {:type=>:request}],
     [:include, Capybara::RSpecMatchers, {:type=>:acceptance}],
     [:include,
      RSpec::Rails::ControllerExampleGroup,
      {:type=>:controller,
       :example_group=>{:file_path=>/spec[\\\/]controllers/}}],
     [:include,
      RSpec::Rails::HelperExampleGroup,
      {:type=>:helper, :example_group=>{:file_path=>/spec[\\\/]helpers/}}],
     [:include,
      RSpec::Rails::MailerExampleGroup,
      {:type=>:mailer, :example_group=>{:file_path=>/spec[\\\/]mailers/}}],
     [:include,
      RSpec::Rails::ModelExampleGroup,
      {:type=>:model, :example_group=>{:file_path=>/spec[\\\/]models/}}],
     [:include,
      RSpec::Rails::RequestExampleGroup,
      {:type=>:request,
       :example_group=>{:file_path=>/spec[\\\/](requests|integration)/}}],
     [:include,
      RSpec::Rails::RoutingExampleGroup,
      {:type=>:routing, :example_group=>{:file_path=>/spec[\\\/]routing/}}],
     [:include,
      RSpec::Rails::ViewExampleGroup,
      {:type=>:view, :example_group=>{:file_path=>/spec[\\\/]views/}}],
     [:include, Devise::TestHelpers, {:type=>:controller}],
     [:include, RequestHelpers, {:type=>:request}],
     [:include, GlobalSpecHelpers, {}]]}>

(that's from my project). So Capybara::DSL gets included before RSpec::Rails::RequestExampleGroup. The former has :type => :request filter, but the type itself is set when the latter is included. So Capybara::DSL module is rejected by the filter and not included.

In your test example, with double describe block, module inclusion occurs twice. The second time type is already set, Capybara::DSL gets included, everything works.

The fix should be darn simple (haven't got more time right now, but I'll come up with it in the case you don't fix it first :)

Contributor

be9 commented May 3, 2011

So, I found it. The example that also breaks for your example app:

require "spec_helper"
describe "things" do
    it "works with visit" do
      go_things
      page.status_code.should eq(200)
    end
end

The cause of the bug is as follows. If we look at the :include_or_extend_modules array in rspec configuration, we see following:

   [[:extend, RSpec::Matchers::DSL, {}],
     [:include, RSpec::Rails::FixtureSupport, {}],
     [:include, RSpec::Rails::Mocks, {}],
     [:include, Capybara::Features, {:capybara_feature=>true}],
     [:include, Capybara::DSL, {:type=>:request}],
     [:include, Capybara::DSL, {:type=>:acceptance}],
     [:include, Capybara::RSpecMatchers, {:type=>:request}],
     [:include, Capybara::RSpecMatchers, {:type=>:acceptance}],
     [:include,
      RSpec::Rails::ControllerExampleGroup,
      {:type=>:controller,
       :example_group=>{:file_path=>/spec[\\\/]controllers/}}],
     [:include,
      RSpec::Rails::HelperExampleGroup,
      {:type=>:helper, :example_group=>{:file_path=>/spec[\\\/]helpers/}}],
     [:include,
      RSpec::Rails::MailerExampleGroup,
      {:type=>:mailer, :example_group=>{:file_path=>/spec[\\\/]mailers/}}],
     [:include,
      RSpec::Rails::ModelExampleGroup,
      {:type=>:model, :example_group=>{:file_path=>/spec[\\\/]models/}}],
     [:include,
      RSpec::Rails::RequestExampleGroup,
      {:type=>:request,
       :example_group=>{:file_path=>/spec[\\\/](requests|integration)/}}],
     [:include,
      RSpec::Rails::RoutingExampleGroup,
      {:type=>:routing, :example_group=>{:file_path=>/spec[\\\/]routing/}}],
     [:include,
      RSpec::Rails::ViewExampleGroup,
      {:type=>:view, :example_group=>{:file_path=>/spec[\\\/]views/}}],
     [:include, Devise::TestHelpers, {:type=>:controller}],
     [:include, RequestHelpers, {:type=>:request}],
     [:include, GlobalSpecHelpers, {}]]}>

(that's from my project). So Capybara::DSL gets included before RSpec::Rails::RequestExampleGroup. The former has :type => :request filter, but the type itself is set when the latter is included. So Capybara::DSL module is rejected by the filter and not included.

In your test example, with double describe block, module inclusion occurs twice. The second time type is already set, Capybara::DSL gets included, everything works.

The fix should be darn simple (haven't got more time right now, but I'll come up with it in the case you don't fix it first :)

be9 added a commit to be9/rspec-rails that referenced this issue May 3, 2011

Require capybara after rspec/rails/example
Otherwise Capybara's includes are not set correctly.

Fixes #360
@be9

This comment has been minimized.

Show comment
Hide comment
@be9

be9 May 3, 2011

Contributor

Fix is available as pull request #362

Contributor

be9 commented May 3, 2011

Fix is available as pull request #362

@dchelimsky dchelimsky closed this in d0e790e May 4, 2011

@tommysqueak

This comment has been minimized.

Show comment
Hide comment
@tommysqueak

tommysqueak May 26, 2011

I've got the same isssue (undefined method `visit' etc). I'm using capybara 1.0.0.beta1 I've got the rspec-rails and dependencies in my gemfile pointing to github. So I would have thought I'd have the latest, which includes the above commit? I have require 'rspec/rails' in my spec_helper.rb. It's in a new project I'm just getting started with. I'm also just getting started with rails, so I'm totally lost. Any help would be much appreciated.

I've got the same isssue (undefined method `visit' etc). I'm using capybara 1.0.0.beta1 I've got the rspec-rails and dependencies in my gemfile pointing to github. So I would have thought I'd have the latest, which includes the above commit? I have require 'rspec/rails' in my spec_helper.rb. It's in a new project I'm just getting started with. I'm also just getting started with rails, so I'm totally lost. Any help would be much appreciated.

@jackkinsella

This comment has been minimized.

Show comment
Hide comment
@jackkinsella

jackkinsella Aug 31, 2011

I ran into the same problem today with rspec-rails 2.6.1 and capybara 1.0.0

#spec_helper.rb

require 'rubygems'
require 'spork'
require "email_spec"
ENV["RAILS_ENV"] ||= 'test'     
Spork.prefork do
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end
Spork.each_run do
  Dir[Rails.root.join("site/app/**/*.rb")].each {|f| require f}     
  require "#{Rails.root}/spec/factories.rb"
end

I ran into the same problem today with rspec-rails 2.6.1 and capybara 1.0.0

#spec_helper.rb

require 'rubygems'
require 'spork'
require "email_spec"
ENV["RAILS_ENV"] ||= 'test'     
Spork.prefork do
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end
Spork.each_run do
  Dir[Rails.root.join("site/app/**/*.rb")].each {|f| require f}     
  require "#{Rails.root}/spec/factories.rb"
end
@jackkinsella

This comment has been minimized.

Show comment
Hide comment
@jackkinsella

jackkinsella Aug 31, 2011

Problem was due to the 'steak' gem. Once I removed it the Capybara methods worked once again.

Problem was due to the 'steak' gem. Once I removed it the Capybara methods worked once again.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Aug 31, 2011

Member

@jackkinsella - glad to hear it, though a lot of people like to use steak so I'd like to understand why that doesn't work.

As an aside, I'd recommend the following changes to your spork config in spec_helper:

#spec_helper.rb

require 'rubygems'
require 'spork'
ENV["RAILS_ENV"] ||= 'test'

Spork.prefork do
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'email_spec' # shouldn't really need this if it's in the :test group in the Gemfile
end

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

This assumes moving spec/factories.rb to spec/support. You don't need to require anything from app/** because Rails does that for you.

Member

dchelimsky commented Aug 31, 2011

@jackkinsella - glad to hear it, though a lot of people like to use steak so I'd like to understand why that doesn't work.

As an aside, I'd recommend the following changes to your spork config in spec_helper:

#spec_helper.rb

require 'rubygems'
require 'spork'
ENV["RAILS_ENV"] ||= 'test'

Spork.prefork do
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'email_spec' # shouldn't really need this if it's in the :test group in the Gemfile
end

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

This assumes moving spec/factories.rb to spec/support. You don't need to require anything from app/** because Rails does that for you.

@jackkinsella

This comment has been minimized.

Show comment
Hide comment
@jackkinsella

jackkinsella Sep 1, 2011

Thanks Dave; I hadn't thought of putting the factories into the support dir. I'm aware that I don't need to require anything from app/, but I have a gem in site/app/ which I'm actively developing.

I decided against using Steak for the same reason I've stopped using Cucumber: unwarranted additional abstraction. Rspec and Capybara do the same job with less lines of code and with fewer configuration headaches.

Thanks Dave; I hadn't thought of putting the factories into the support dir. I'm aware that I don't need to require anything from app/, but I have a gem in site/app/ which I'm actively developing.

I decided against using Steak for the same reason I've stopped using Cucumber: unwarranted additional abstraction. Rspec and Capybara do the same job with less lines of code and with fewer configuration headaches.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Sep 1, 2011

Member

Ah - missed that it was site/app. Cheers.

Member

dchelimsky commented Sep 1, 2011

Ah - missed that it was site/app. Cheers.

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Feb 16, 2012

I'm still seeing this with the latest rspec and rspec-rails. The visit method is undefined unless I nest my tests inside a second describe. As a result, I wrap my tests in a meaningless secondary describe.

I am using spork, so perhaps that has something to do with it?

require 'rubygems'
require 'spork'

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

  # use capybara-webkit
  Capybara.javascript_driver = :webkit

  # calls to DatabaseCleaner.clean will use truncation
  # otherwise, we will still use transactional fixtures
  DatabaseCleaner.strategy = :truncation

  # 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|
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.include ModelMacros
    config.include ActionView::TestCase::Behavior, :example_group => {:file_path => %r{spec/presenters}}
    # config.include FactoryGirl::Syntax::Methods

    config.before(:suite) do
      # Create the admin user
      Factory(:user, :login => ENV['REMOTE_USER'], :roles_mask => 1)
    end
  end
end

Spork.each_run do
  # verifies we start with a clean database each run
  DatabaseCleaner.clean

  # require factories
  require 'factory_girl_rails'
  RSpec.configure do |config|
    config.include FactoryGirl::Syntax::Methods
  end
end

# This allows us to use transactional fixtures when
# using selenium/capybara-webkit by forcing them to use
# the same thread as capybara.
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def current_connection_id
    Thread.main.object_id
  end
end

I'm still seeing this with the latest rspec and rspec-rails. The visit method is undefined unless I nest my tests inside a second describe. As a result, I wrap my tests in a meaningless secondary describe.

I am using spork, so perhaps that has something to do with it?

require 'rubygems'
require 'spork'

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

  # use capybara-webkit
  Capybara.javascript_driver = :webkit

  # calls to DatabaseCleaner.clean will use truncation
  # otherwise, we will still use transactional fixtures
  DatabaseCleaner.strategy = :truncation

  # 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|
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.mock_with :rspec
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.include ModelMacros
    config.include ActionView::TestCase::Behavior, :example_group => {:file_path => %r{spec/presenters}}
    # config.include FactoryGirl::Syntax::Methods

    config.before(:suite) do
      # Create the admin user
      Factory(:user, :login => ENV['REMOTE_USER'], :roles_mask => 1)
    end
  end
end

Spork.each_run do
  # verifies we start with a clean database each run
  DatabaseCleaner.clean

  # require factories
  require 'factory_girl_rails'
  RSpec.configure do |config|
    config.include FactoryGirl::Syntax::Methods
  end
end

# This allows us to use transactional fixtures when
# using selenium/capybara-webkit by forcing them to use
# the same thread as capybara.
ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do
  def current_connection_id
    Thread.main.object_id
  end
end
@aia

This comment has been minimized.

Show comment
Hide comment
@aia

aia Feb 27, 2012

Getting the same error with rspec-rails (2.8.1) and capybara (1.1.2).
Have to manually include Capybara::DSL.

aia commented Feb 27, 2012

Getting the same error with rspec-rails (2.8.1) and capybara (1.1.2).
Have to manually include Capybara::DSL.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Feb 27, 2012

Member

@derekprior, @ala I can't repro this on my system w/ Mac OS and ruby 1.9.3. What OS and ruby versions are you running?

Member

dchelimsky commented Feb 27, 2012

@derekprior, @ala I can't repro this on my system w/ Mac OS and ruby 1.9.3. What OS and ruby versions are you running?

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Feb 27, 2012

I'm running OS X 10.6.8, with Ruby 1.9.2-p290. I'll take a shot at distilling a simplified reproduction.

I'm running OS X 10.6.8, with Ruby 1.9.2-p290. I'll take a shot at distilling a simplified reproduction.

@aia

This comment has been minimized.

Show comment
Hide comment
@aia

aia Feb 27, 2012

Running on OS X too:

rvm -v
rvm 1.10.3 by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.beginrescueend.com/]

ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

aia commented Feb 27, 2012

Running on OS X too:

rvm -v
rvm 1.10.3 by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.beginrescueend.com/]

ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Feb 27, 2012

Okay, @dchelimsky, I think I boiled it down to a base case (at least in my case). Please see this repository:
https://github.com/derekprior/rspec_rails_bug

The reproduction succeeds (visit is undefined) if I require capyabara/rspec before rspec/rails in spec_helper.rb. Interestingly, it seems completely unnecessary to require capyabara/rspec at all as removing that line completely makes all tests pass. Requiring rspec/rails first also causes all tests to pass.

The repository I linked to will produce a failing spec on 1.9.2-p290 and 1.9.3-p0. I suspect the same is true of 1.8.7, but I didn't have time to correct the hash syntax and re-run the tests there.

Seems like "leave it to rspec-rails to require capyabara" is the way to go. I think that line was in my spec_helper from days prior to whatever version of rspec-rails added the automatic require.

Okay, @dchelimsky, I think I boiled it down to a base case (at least in my case). Please see this repository:
https://github.com/derekprior/rspec_rails_bug

The reproduction succeeds (visit is undefined) if I require capyabara/rspec before rspec/rails in spec_helper.rb. Interestingly, it seems completely unnecessary to require capyabara/rspec at all as removing that line completely makes all tests pass. Requiring rspec/rails first also causes all tests to pass.

The repository I linked to will produce a failing spec on 1.9.2-p290 and 1.9.3-p0. I suspect the same is true of 1.8.7, but I didn't have time to correct the hash syntax and re-run the tests there.

Seems like "leave it to rspec-rails to require capyabara" is the way to go. I think that line was in my spec_helper from days prior to whatever version of rspec-rails added the automatic require.

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Feb 27, 2012

Actually, it appears that test case isn't true to my actual project issue. In my actual project, neither removing or re-ordering the requires fixes anything. Calls to visit still must be at least 2 describes deep. Odd.

I'll keep looking...

Actually, it appears that test case isn't true to my actual project issue. In my actual project, neither removing or re-ordering the requires fixes anything. Calls to visit still must be at least 2 describes deep. Odd.

I'll keep looking...

@aia

This comment has been minimized.

Show comment
Hide comment
@aia

aia Feb 27, 2012

#
#  *Working*
# spec/controller/pages_controller_spec.rb
#
require 'spec_helper'

describe PagesController do
  render_views

  describe "GET 'home'" do
    it "should be successful" do
      visit '/'
      response.should be_successful
    end
  end
end
#
# *Not Working*
# spec/requests/user_registration_spec.rb
#
require 'spec_helper'

describe "user registration" do  
  it "should serve the registration page" do
    visit '/signup'
    page.should have_content("Sign Up")
  end
end

aia commented Feb 27, 2012

#
#  *Working*
# spec/controller/pages_controller_spec.rb
#
require 'spec_helper'

describe PagesController do
  render_views

  describe "GET 'home'" do
    it "should be successful" do
      visit '/'
      response.should be_successful
    end
  end
end
#
# *Not Working*
# spec/requests/user_registration_spec.rb
#
require 'spec_helper'

describe "user registration" do  
  it "should serve the registration page" do
    visit '/signup'
    page.should have_content("Sign Up")
  end
end
@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Feb 28, 2012

Member

@ala - what's not working? You're getting undefined method on visit? Or something else?

Member

dchelimsky commented Feb 28, 2012

@ala - what's not working? You're getting undefined method on visit? Or something else?

@aia

This comment has been minimized.

Show comment
Hide comment
@aia

aia Feb 28, 2012

@dchelimsky yes, getting the undefined visit method.

aia commented Feb 28, 2012

@dchelimsky yes, getting the undefined visit method.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Feb 28, 2012

Member

@ala is your repo public?

Member

dchelimsky commented Feb 28, 2012

@ala is your repo public?

@aia

This comment has been minimized.

Show comment
Hide comment
@aia

aia Feb 28, 2012

@dchelimsky unfortunately no. Let me see if I can cut out that example. Should be a pretty basic Rails 3.2 app.

aia commented Feb 28, 2012

@dchelimsky unfortunately no. Let me see if I can cut out that example. Should be a pretty basic Rails 3.2 app.

@duckpond

This comment has been minimized.

Show comment
Hide comment
@duckpond

duckpond Mar 2, 2012

I'm seeing this identical issue. We had

  config.include RSpec::Rails::RequestExampleGroup, :example_group => { :file_path => /spec\/externals/ }

in our spec_helper but it was still getting undefined method 'visit'.

duckpond commented Mar 2, 2012

I'm seeing this identical issue. We had

  config.include RSpec::Rails::RequestExampleGroup, :example_group => { :file_path => /spec\/externals/ }

in our spec_helper but it was still getting undefined method 'visit'.

@dnd

This comment has been minimized.

Show comment
Hide comment
@dnd

dnd Mar 23, 2012

Just another mention that I'm having the same problem described by aia. If I nest the visit call two describes deep then it works fine. This is on rspec* 2.9.0

dnd commented Mar 23, 2012

Just another mention that I'm having the same problem described by aia. If I nest the visit call two describes deep then it works fine. This is on rspec* 2.9.0

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Mar 27, 2012

Member

Folks - I need a repro. Can anybody who is experiencing this problem post a repo to github that I can clone, bundle, run, and see the issue?

Member

dchelimsky commented Mar 27, 2012

Folks - I need a repro. Can anybody who is experiencing this problem post a repo to github that I can clone, bundle, run, and see the issue?

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Mar 27, 2012

I will try to cleans my app of anything proprietary and post the repro

I will try to cleans my app of anything proprietary and post the repro

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Mar 27, 2012

Alright, here's a repo:
https://github.com/derekprior/rspec-rails-capybara-bug

In my case it seems that the gem 'capybara-screenshot' is causing the problem. If I remove this gem from the gemfile, both specs pass. I've tried keeping it in with various gem orderings, but I have not had any success. I haven't yet dug into this gem to see what's going on.

Curious if other folks having problems also have this gem?

Alright, here's a repo:
https://github.com/derekprior/rspec-rails-capybara-bug

In my case it seems that the gem 'capybara-screenshot' is causing the problem. If I remove this gem from the gemfile, both specs pass. I've tried keeping it in with various gem orderings, but I have not had any success. I haven't yet dug into this gem to see what's going on.

Curious if other folks having problems also have this gem?

@dnd

This comment has been minimized.

Show comment
Hide comment
@dnd

dnd Mar 27, 2012

I can't try removing that gem from mine at the moment, but I am also using 'capybara-screenshot', version 0.1.10 to be specific.

dnd commented Mar 27, 2012

I can't try removing that gem from mine at the moment, but I am also using 'capybara-screenshot', version 0.1.10 to be specific.

@derekprior

This comment has been minimized.

Show comment
Hide comment
@derekprior

derekprior Mar 27, 2012

Here's what capybara-screenshot is doing on load:
https://github.com/mattheworiordan/capybara-screenshot/blob/master/lib/capybara-screenshot.rb

I don't know enough about the internals to say what the problem is off hand, but perhaps David or someone else does?

Here's what capybara-screenshot is doing on load:
https://github.com/mattheworiordan/capybara-screenshot/blob/master/lib/capybara-screenshot.rb

I don't know enough about the internals to say what the problem is off hand, but perhaps David or someone else does?

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Mar 27, 2012

Member

It's definitely a load order integration bug. I don't understand it yet, but you can work around it by doing the following:

# in Gemfile
  gem 'capybara-screenshot', :require => false

# in spec/spec_helper.rb
  require 'rspec/rails'
  require 'capybara-screenshot'
Member

dchelimsky commented Mar 27, 2012

It's definitely a load order integration bug. I don't understand it yet, but you can work around it by doing the following:

# in Gemfile
  gem 'capybara-screenshot', :require => false

# in spec/spec_helper.rb
  require 'rspec/rails'
  require 'capybara-screenshot'
@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Mar 27, 2012

Member

Also - @derekprior thanks for posting the app - that helped me see the bug, figure out the workaround, and will eventually lead to a fix.

Member

dchelimsky commented Mar 27, 2012

Also - @derekprior thanks for posting the app - that helped me see the bug, figure out the workaround, and will eventually lead to a fix.

@5minpause

This comment has been minimized.

Show comment
Hide comment
@5minpause

5minpause May 10, 2012

Unfortunately this workaround doesn't do it for me. I still get

NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>

The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'

edit: If i put include Capybara::DSL inside my describe block it works.

Unfortunately this workaround doesn't do it for me. I still get

NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fbfeb535298>

The repo is public under: https://github.com/ikusei/Goldencobra_Newsletter
You need to look at the branch '28817499-subscribe'

edit: If i put include Capybara::DSL inside my describe block it works.

@bigtunacan

This comment has been minimized.

Show comment
Hide comment
@bigtunacan

bigtunacan Jul 23, 2012

I see this is marked as closed, but I'm having this same issue

I see this is marked as closed, but I'm having this same issue

@5minpause

This comment has been minimized.

Show comment
Hide comment
@5minpause

5minpause Jul 23, 2012

Did you see my edit? After doing that it worked for me.

Did you see my edit? After doing that it worked for me.

@bigtunacan

This comment has been minimized.

Show comment
Hide comment
@bigtunacan

bigtunacan Jul 24, 2012

@jazzgumpy , Thanks that works for me as well using include Capybara::DSL within my outer describe

@jazzgumpy , Thanks that works for me as well using include Capybara::DSL within my outer describe

@henrik

This comment has been minimized.

Show comment
Hide comment
@henrik

henrik Jul 31, 2012

Contributor

@dchelimsky I started on a new app and ran into this, so this is a very small (if not minimal) failure case with RSpec 2.11: https://dl.dropbox.com/u/546793/joblog.zip

Note that there is a workaround (config.include Capybara::DSL) in spec/spec_helper.rb:19, so comment that out to (hopefully) reproduce.

(And I will of course make sure to change the secret_token.)

Contributor

henrik commented Jul 31, 2012

@dchelimsky I started on a new app and ran into this, so this is a very small (if not minimal) failure case with RSpec 2.11: https://dl.dropbox.com/u/546793/joblog.zip

Note that there is a workaround (config.include Capybara::DSL) in spec/spec_helper.rb:19, so comment that out to (hopefully) reproduce.

(And I will of course make sure to change the secret_token.)

@henrik

This comment has been minimized.

Show comment
Hide comment
@henrik

henrik Jul 31, 2012

Contributor

@bigtunacan @jazzgumpy A slightly nicer workaround is to put

config.include Capybara::DSL

in spec_helper.rb, inside the config block.

Contributor

henrik commented Jul 31, 2012

@bigtunacan @jazzgumpy A slightly nicer workaround is to put

config.include Capybara::DSL

in spec_helper.rb, inside the config block.

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Aug 1, 2012

Member

@henrik the directory is requests (plural), not request. Try that and it works as expected.

Member

dchelimsky commented Aug 1, 2012

@henrik the directory is requests (plural), not request. Try that and it works as expected.

@henrik

This comment has been minimized.

Show comment
Hide comment
@henrik

henrik Aug 1, 2012

Contributor

@dchelimsky Thanks, that did work. Sorry for bothering you with such a silly mistake. Maybe @jazzgumpy and @bigtunacan made similar mistakes.

Would be nice if it was harder to make a mistake, or easier to recover from it, but I can't think of how, other than maybe emphasizing it in the docs. Pull request for that: #585

Contributor

henrik commented Aug 1, 2012

@dchelimsky Thanks, that did work. Sorry for bothering you with such a silly mistake. Maybe @jazzgumpy and @bigtunacan made similar mistakes.

Would be nice if it was harder to make a mistake, or easier to recover from it, but I can't think of how, other than maybe emphasizing it in the docs. Pull request for that: #585

@dchelimsky

This comment has been minimized.

Show comment
Hide comment
@dchelimsky

dchelimsky Aug 1, 2012

Member

I merged your pull request, so that should help. Thx. Also, the integration test generator puts it in the right place.

Member

dchelimsky commented Aug 1, 2012

I merged your pull request, so that should help. Thx. Also, the integration test generator puts it in the right place.

@wojtha

This comment has been minimized.

Show comment
Hide comment
@wojtha

wojtha Nov 16, 2012

I've run in this issue after update to capybara 2.0. First I implemented the henrik's workaround by mixing Capybara::DSL methods to Rspec but now I've found this in the Capybara 2.0 update notes:

To upgrade to capybara 2.0, you'll need to do a few things:

Move any tests that use capybara from spec/requests to spec/features. Capybara tests use the visit method and usually assert against page. Alternatively, you can keep using capybara in request specs, but you'll need to manually mix in the methods.

See http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

I have my tests in the old path spec/requests of course ...

Might be it could help someone.

wojtha commented Nov 16, 2012

I've run in this issue after update to capybara 2.0. First I implemented the henrik's workaround by mixing Capybara::DSL methods to Rspec but now I've found this in the Capybara 2.0 update notes:

To upgrade to capybara 2.0, you'll need to do a few things:

Move any tests that use capybara from spec/requests to spec/features. Capybara tests use the visit method and usually assert against page. Alternatively, you can keep using capybara in request specs, but you'll need to manually mix in the methods.

See http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html

I have my tests in the old path spec/requests of course ...

Might be it could help someone.

@reejosamuel

This comment has been minimized.

Show comment
Hide comment
@reejosamuel

reejosamuel Dec 17, 2012

for others facing the issue, renaming request to requests is no longer the problem, all requests need to be moved to features folder

for others facing the issue, renaming request to requests is no longer the problem, all requests need to be moved to features folder

@matt-hwy1

This comment has been minimized.

Show comment
Hide comment
@matt-hwy1

matt-hwy1 Dec 19, 2012

2wojtha and @reejosamuel, thank you. I renamed my requests dir to features and that fixed it!

2wojtha and @reejosamuel, thank you. I renamed my requests dir to features and that fixed it!

@pcerioli

This comment has been minimized.

Show comment
Hide comment
@pcerioli

pcerioli Dec 24, 2012

thank you @wojtha.
That fixed the issue for me.

thank you @wojtha.
That fixed the issue for me.

@george

This comment has been minimized.

Show comment
Hide comment
@george

george Jan 9, 2013

renaming requests to features worked here, too. Thx.

george commented Jan 9, 2013

renaming requests to features worked here, too. Thx.

@eschaefer

This comment has been minimized.

Show comment
Hide comment

ditto for me @george

@alindeman

This comment has been minimized.

Show comment
Hide comment
@alindeman

alindeman Jan 16, 2013

Contributor

For reference, it's documented in Capybara.md and this blog post

Contributor

alindeman commented Jan 16, 2013

For reference, it's documented in Capybara.md and this blog post

@Chrisgo-75

This comment has been minimized.

Show comment
Hide comment
@Chrisgo-75

Chrisgo-75 Mar 6, 2013

renamed requests to features. Problem fixed. Thank you.

renamed requests to features. Problem fixed. Thank you.

@IamNaN

This comment has been minimized.

Show comment
Hide comment
@IamNaN

IamNaN Mar 21, 2013

I'm using rails 4.0.0.beta1. Renamed the requests folder to features, but still needed to add config.include Capybara::DSL to spec_helper.rb.

IamNaN commented Mar 21, 2013

I'm using rails 4.0.0.beta1. Renamed the requests folder to features, but still needed to add config.include Capybara::DSL to spec_helper.rb.

@emad-elsaid

This comment has been minimized.

Show comment
Hide comment
@emad-elsaid

emad-elsaid Mar 24, 2013

i got the same problem and fixed with 2 way :
1- renaming the "requests" directory to "features"
2- the work around of adding Capybara DSL to my rspec helper mentioned above

thanks guys

i got the same problem and fixed with 2 way :
1- renaming the "requests" directory to "features"
2- the work around of adding Capybara DSL to my rspec helper mentioned above

thanks guys

@dmxforever

This comment has been minimized.

Show comment
Hide comment
@dmxforever

dmxforever Mar 31, 2013

renaming "requests" to "features" fix it. rails 4

renaming "requests" to "features" fix it. rails 4

@lucasdavila

This comment has been minimized.

Show comment
Hide comment
@lucasdavila

lucasdavila Apr 11, 2013

👍 renaming "requests" to "features" fixed it, on Rails 3.

👍 renaming "requests" to "features" fixed it, on Rails 3.

@longli

This comment has been minimized.

Show comment
Hide comment
@longli

longli May 3, 2013

1、add "gem "rspec-rails", "> 2.0" and gem 'capybara', '>1.1.2'" in Gemfile
2、bundle install
3、add “config.include Capybara::DSL” in spec_helper.rb
OK! Good luck for you!

longli commented May 3, 2013

1、add "gem "rspec-rails", "> 2.0" and gem 'capybara', '>1.1.2'" in Gemfile
2、bundle install
3、add “config.include Capybara::DSL” in spec_helper.rb
OK! Good luck for you!

@flyfy1

This comment has been minimized.

Show comment
Hide comment
@flyfy1

flyfy1 Jun 5, 2013

Hmm I think it might be helpful to add the poing number 3 into the README.md file of capybara so that when people use they don't need to do search for the problem again.

flyfy1 commented Jun 5, 2013

Hmm I think it might be helpful to add the poing number 3 into the README.md file of capybara so that when people use they don't need to do search for the problem again.

@nathanhamilton

This comment has been minimized.

Show comment
Hide comment
@nathanhamilton

nathanhamilton Jan 30, 2014

Renaming the spec/requests folder to spec/features got things rolling for me too. Thanks for the help.

Renaming the spec/requests folder to spec/features got things rolling for me too. Thanks for the help.

@StevenClontz

This comment has been minimized.

Show comment
Hide comment
@StevenClontz

StevenClontz Feb 4, 2014

Renaming the spec/requests folder to spec/features got things rolling for me too. Thanks for the help.

+1

Renaming the spec/requests folder to spec/features got things rolling for me too. Thanks for the help.

+1

@lukexuanliu

This comment has been minimized.

Show comment
Hide comment
@lukexuanliu

lukexuanliu Sep 16, 2014

I followed all the different work-around (or I think I did) in this post and still could not get it working. I still see the NoMethodError. My repo is a really basic practice website. Please help at
https://github.com/lukexuanliu/CornellBlog/tree/static_pages
Thanks everyone!

I followed all the different work-around (or I think I did) in this post and still could not get it working. I still see the NoMethodError. My repo is a really basic practice website. Please help at
https://github.com/lukexuanliu/CornellBlog/tree/static_pages
Thanks everyone!

@JonRowe

This comment has been minimized.

Show comment
Hide comment
@JonRowe

JonRowe Sep 16, 2014

Member

You need to add a require 'rails_helper' line beneath require 'spec_helper', in future please use the RSpec mailing list for questions like this, not issues.

Member

JonRowe commented Sep 16, 2014

You need to add a require 'rails_helper' line beneath require 'spec_helper', in future please use the RSpec mailing list for questions like this, not issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment