未定义的局部变量或方法“root_path" [英] undefined local variable or method `root_path'

查看:58
本文介绍了未定义的局部变量或方法“root_path"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 rspec 文件中有以下代码块位于/spec 文件夹的根目录.

I have the following block of code in my rspec file located in the root of the /spec folder.

require 'spec_helper'

describe "home" do

 subject { page }

 before(:each) { visit root_path }

 describe "page" do
 it { should have_title('My Page')}
 end

end

当我运行它时,我得到

undefined local variable or method `root_path'

这没有任何意义.当我遵循 rails 教程时,类似的设置工作得很好.有人可以帮忙吗?

Which doesn't make any sense. When I followed the rails tutorial a similar set up worked just fine. Can anyone help?

我的路线包括

root "static#home"

编辑 2:

重新打开这个话题.将我的根声明移到顶部并没有解决问题.

Reopening this topic. Moving my root declaration to the top did not fix it.

编辑 3:

在我的 rspec 配置中包含 url_helpers 是有效的.我以前从来没有这样做过.谁能回答为什么这有效?

What worked was including url_helpers in my rspec config. I've never had to do this before. can anyone answer why this worked?

推荐答案

默认情况下,规范中不提供命名路由.将以下代码添加到 spec_helper.rb:

Named routes are not available in specs by default. Add the following code to spec_helper.rb:

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

这篇关于未定义的局部变量或方法“root_path"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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