【问题标题】:Rspec test - custom route path results in not recognized by rspec "undefined local variable/method"Rspec 测试 - 自定义路由路径导致 rspec “未定义的局部变量/方法”无法识别
【发布时间】:2014-01-31 10:59:08
【问题描述】:

我定义了一个在应用程序中有效的自定义路由,但由于某种原因在 rspec 中无效。

/app/config/routes.rb

  get '/signup',   to: 'users#new',             as: :signup

注册页面视图文件具有“signup_path”链接,并且可以正常工作。 /app/views/users/new.html.rb

<% provide(:title, 'Sign Up') %>
<h1>Sign Up</h1>
<%= link_to "signup_path", signup_path %>

但是在 rspec 中,使用这个测试

require 'spec_helper'

describe "UserPages -" do

  subject { page }
  let(:base_title) { "Ruby on Rails Tutorial Sample App" }

  describe "Signup Page" do
    before { visit signup_path }

    it { should have_selector('h1', text: 'Sign Up') }
    it { should have_title(full_title('Sign Up')) }
  end

end

结果是一个失败的测试,它说

"signup_path 是一个未定义的局部变量或方法"

我正在使用水豚 2.1.0

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

    除非您将其添加到 spec_helper.rb,否则您无法从测试中访问命名路由

    Rspec.configure do |config|
      config.include Rails.application.routes.url_helpers
      ...
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多