Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined local variable or method capybara rspec

I keep getting this strange error when I use capybara methods outside of a before block:

$ rspec . -e "PasswordResets"
/spec/requests/password_resets_spec.rb:9:in `block (2 levels) in <top (required)>': undefined local variable or method `root_path' for #<Class:0x00000003bfa100> (NameError)

root_path does exists. I have multiple other tests that work. But it appears I can only use Capybara when the methods are inside a before block. This works:

require 'spec_helper'
describe "PasswordResets" do
  subject { page }
  describe "it emails user when requesting a password reset" do
    before do
      visit root_path
    end
  end
end

This produces the error:

require 'spec_helper'
describe "PasswordResets" do
  subject { page }
  describe "it emails user when requesting a password reset" do
    visit root_path
  end
end

This works:

require 'spec_helper'
describe "PasswordResets" do
  it "emails user when requesting a password reset" do
    visit root_path
  end
end

What I want to do is use the subject { page } but be able to use capybara without having to use a before do end block. Any idea what I'm doing wrong?

My spec_helper.rb file:

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

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

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false
end

I'm on Rails 3.2 with Capybara 1.1.2, Rspec 2.8.1.

like image 953
Mohamad Avatar asked Mar 13 '12 18:03

Mohamad


1 Answers

You can't put test code directly in a describe. The scope is different. Put it in a before or an it block.

like image 117
Austin Taylor Avatar answered Nov 16 '22 06:11

Austin Taylor