简体   繁体   English

为什么我不能从Rspec测试中访问Rails的URL帮助器?

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

Initially I wrote my tests with Cucumber and Capybara, and that worked fine. 最初,我是用Cucumber和Capybara编写测试的,效果很好。

Then I switched to using Rpsec and Capybara, and that worked fine too. 然后我转而使用Rpsec和Capybara,效果也很好。

Then I deleted my gemlock file while trying to get the asset pipeline working and suddenly the URL helpers in my tests stopped working. 然后,在尝试使资产管道正常工作时,我删除了gemlock文件,突然测试中的URL帮助程序停止工作。

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. 我想让URL帮助程序重新工作,以便在完成资产管道的实现后,我的测试可以指导我。 How can I fix this? 我怎样才能解决这个问题?

All my tests are in /spec/features. 我所有的测试都在/ spec / features中。

Relevant software follows... 相关软件如下...

/spec/spec_helper.rb: /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: /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

解决方案是升级到最新的rspec-rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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