为什么我无法通过Rspec测试访问Rails的URL帮助器? [英] Why can't I access Rails' URL helpers from my Rspec tests?

查看:65
本文介绍了为什么我无法通过Rspec测试访问Rails的URL帮助器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我是用Cucumber和Capybara编写测试的,并且效果很好。

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

然后我改用Rpsec和Capybara,效果也很好。

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

然后我在尝试使资产管道正常工作时删除了gemlock文件,突然测试中的URL帮助程序停止工作。

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

示例错误:

 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)>'

我想使URL帮助程序重新工作,以便在完成资产管道实现时我的测试可以指导我。我该如何解决?

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?

我所有的测试都在/ spec / features中。

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


推荐答案

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

The solution was to upgrade to the latest rspec-rails

这篇关于为什么我无法通过Rspec测试访问Rails的URL帮助器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆