Why can't I access Rails' URL helpers from my Rspec tests?

一曲冷凌霜 提交于 2019-12-25 13:09:18

问题


Initially I wrote my tests with Cucumber and Capybara, and that worked fine.

Then I switched to using Rpsec and Capybara, and that worked fine too.

Then I deleted my gemlock file while trying to get the asset pipeline working and suddenly the URL helpers in my tests stopped working.

Sample error:

 Failure/Error: visit report_areas_path(@report)
 NoMethodError:
   undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f978c64e058>
 # ./spec/features/area_spec.rb:12:in `block (3 levels) in <top (required)>'

I'd like to get the URL helpers working again so my tests can guide me as I finish implementing the asset pipeline. How can I fix this?

All my tests are in /spec/features.

Relevant software follows...

/spec/spec_helper.rb:

require 'rubygems'
require 'spork'

Spork.prefork do
end

Spork.each_run do
  # This code will be run each time you run your specs.    
end    

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

require 'capybara/rspec'
require 'capybara/rails' 
require 'capybara/dsl' 
require 'capybara-screenshot/rspec'

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

RSpec.configure do |config|
  config.mock_with :rspec
  config.fail_fast = true
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.before(:each) { ActionMailer::Base.deliveries.clear }
  config.use_instantiated_fixtures = false 
  config.include(Capybara, :type => :integration) 
end

Gemfile:

source 'http://rubygems.org'

gem 'rails'
gem 'activesupport'
gem 'actionmailer'
gem 'mail'
gem 'railties'
gem 'compass'
gem 'pg'
gem 'sass'
gem 'haml'
gem 'haml-rails'
gem 'susy'
gem 'jquery-rails'
gem 'google-analytics-rails'
gem 'declarative_authorization'
gem 'authlogic', '3.1.0'
gem 'simple_form'
gem 'country-select'
gem 'rails3-generators'
gem 'hirb'
gem 'debugger'
gem 'redis'
gem 'RedCloth'
gem 'friendly_id'
gem 'high_voltage'
gem 'validatious'
gem 'houdini'
gem 'growl-rspec'
gem 'interactive_rspec'
gem "rspec-rerun"

group :development do
  gem 'taps'
  gem "better_errors"
  gem "binding_of_caller"
end

group :test do
  gem 'capybara'
  gem 'rspec-rails', '2.11'
  gem 'database_cleaner', '0.6.7'
  gem 'launchy'
  gem 'spork', '~> 1.0rc'
  gem 'growl'
  gem 'capybara-screenshot'
end

group :assets do
  gem 'sass-rails'
  gem 'coffee-rails'
  gem 'compass-rails'
  gem 'compass-susy-plugin'
  gem 'fancy-buttons'
  gem 'jquery-ui-rails'
  gem 'uglifier'
end

/spec/features/area_spec.rb:

require 'spec_helper'

describe Area do

  before(:each) do
    @report = Report.create(:name => "The Testivate Retail Site Search Benchmark", :short_name => "Search")
  end

  describe "Guest" do

    it 'cannot list' do
      visit report_areas_path(@report)
      page.current_path.should == root_path
    end

  end

  describe "Customer" do

    it 'cannot list' do
      @user = User.create(:username => 'Virginia', :password => 'password', :password_confirmation => 'password', :email => 'example@example.com')
      @role = Role.create(:name => "customer")
      @assignment = Assignment.create(:user_id => @user.id, :role_id => @role.id)
      visit login_path
      fill_in 'Username', :with => @user.username
      fill_in 'Password', :with => @user.password
      click_on 'Login'
      visit report_areas_path(@report)
      page.current_path.should == root_path
    end

  end

  describe "Admin" do

    before(:each) do
      @user = User.create(username: 'Virginia', password: 'pass', password_confirmation: 'pass', email: 'example@example.com')
      @role = Role.create(:name => "admin")    
      @assignment = Assignment.create(user_id: @user.id, role_id: @role.id)
      visit login_path
      fill_in 'Username', :with => @user.username
      fill_in 'Password', :with => @user.password
      click_on 'Login'
      visit report_path(@report)
      click_on "New Area"
      fill_in 'Name', :with => "Search box"
      click_on 'Create Area'
    end

    it 'can create' do
      page.current_path.should == report_area_path(@report, Area.last)
      page.should have_content("Search box")
      page.should have_content("The Testivate Retail Site Search Benchmark")
    end

    it 'can list' do
      click_link 'List Areas'
      page.current_path.should == report_areas_path(@report)
      page.should have_content("Search box")
      page.should have_content("The Testivate Retail Site Search Benchmark")
    end

    it 'can area' do
      click_link 'Edit Area'
      fill_in 'Name', :with => "Search fox"
      click_on 'Update Area'
      page.current_path.should == report_area_path(@report, Area.last)
      page.should have_content("Search fox")
      page.should_not have_content("Search box")
      page.should have_content("The Testivate Retail Site Search Benchmark")
    end

    it 'can delete' do
      click_link 'Delete Area'
      page.current_path.should == report_areas_path(@report)
      page.should_not have_content("Search box")
      page.should have_content("The Testivate Retail Site Search Benchmark")
    end

    describe "must pass validations" do

      before(:each) do
        click_link 'Edit Area'
      end

      it "must have a name" do
        fill_in 'Name', :with => ""
        click_on 'Update Area'
        page.should have_content("Please enter a name for this area.")
      end

    end

  end

end

回答1:


The solution was to upgrade to the latest rspec-rails



来源:https://stackoverflow.com/questions/13852683/why-cant-i-access-rails-url-helpers-from-my-rspec-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!