ruby-on-rails - Rspec 测试 - 自定义路由路径导致 rspec "undefined local variable/method"无法识别

标签 ruby-on-rails testing rspec

我定义了一个在应用程序中工作的自定义路由,但出于某种原因在 rspec 中不工作。

/app/config/routes.rb

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

注册页面 View 文件具有“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 是一个未定义的局部变量或方法”

我使用的是 capybara 2.1.0

最佳答案

除非将其添加到 spec_helper.rb

,否则无法从测试中访问命名路由
Rspec.configure do |config|
  config.include Rails.application.routes.url_helpers
  ...
end

关于ruby-on-rails - Rspec 测试 - 自定义路由路径导致 rspec "undefined local variable/method"无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21478384/

相关文章:

python - 有史以来最没有帮助的错误 : TypeError: unhashable type: 'list'

ruby - 我可以检测我的 rspecs 中的元素(按钮)是否为 "clickable"吗?

java - Maven 没有看到 Mockito 测试?

ruby-on-rails - 无法在本地连接到 Postgresql 服务器

ruby-on-rails - 如何访问 Rails 3 应用程序对象的名称?

ruby-on-rails - Rails - 是两个日期之间的日期吗?

excel - 如何测试处理大量以不可预测格式存储的数据的程序

ruby-on-rails-3 - 使用 rspec 测试 Rails 引擎助手

ruby-on-rails - Rails respond_with & Rspec Controller : Testing unsuccessful update

ruby-on-rails - sort_by 在 Rails 中使用 boolean 值