Ruby on rails 失败/错误:在{visit signup_path}name错误:未定义的局部变量或方法`signup_path';对于#<;RSpec::Core::ExampleGroup
我目前正在学习Rails教程的第七章,似乎还停留在图7.22中。简言之,我无法通过考试。当我跑的时候Ruby on rails 失败/错误:在{visit signup_path}name错误:未定义的局部变量或方法`signup_path';对于#<;RSpec::Core::ExampleGroup,ruby-on-rails,testing,rspec,railstutorial.org,Ruby On Rails,Testing,Rspec,Railstutorial.org,我目前正在学习Rails教程的第七章,似乎还停留在图7.22中。简言之,我无法通过考试。当我跑的时候 bundle exec rspec spec/requests/user_pages_spec.rb 。我看到一堆失败的测试,内容如下: Failures: 1) User pages signup page Failure/Error: before { visit signup_path } NameError: undefined local variabl
bundle exec rspec spec/requests/user_pages_spec.rb
。我看到一堆失败的测试,内容如下:
Failures:
1) User pages signup page
Failure/Error: before { visit signup_path }
NameError:
undefined local variable or method `signup_path' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb2be028410>
# ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages signup page
Failure/Error: before { visit signup_path }
NameError:
undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb2be048ad0>
# ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User pages signup page with invalid information should not create a user
Failure/Error: before { visit signup_path }
NameError:
undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fb2be062e80>
# ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
4) User pages signup page with valid information should create a user
Failure/Error: before { visit signup_path }
NameError:
undefined local variable or method `signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_2:0x007fb2be083158>
# ./user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.00374 seconds
4 examples, 4 failures
Failed examples:
rspec ./user_pages_spec.rb:11 # User pages signup page
rspec ./user_pages_spec.rb:12 # User pages signup page
rspec ./user_pages_spec.rb:18 # User pages signup page with invalid information should not create a user
rspec ./user_pages_spec.rb:32 # User pages signup page with valid information should create a user
Randomized with seed 10291
spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.include Capybara::DSL
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Rails.application.routes.url_helpers
end
以下是user_pages_spec.rb中失败的测试:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before {visit user_path(user)}
it {should have_content(user.name)}
it {should have_title(user.name)}
end
end
在阅读Hartl教程时,如果您遇到
未定义的局部变量或方法
错误,或者您希望了解教程中当前变量或方法的定义,以下技巧将非常有用:
- 去网上看书
- 单击右上角的“查看为单页”链接
- 使用浏览器的搜索命令,从教程中的位置向后搜索有问题的变量/方法名称。(注意:如果有很多,并且您确定这是一个方法,您可以将搜索范围缩小到“def variable_或_method_in_question”)
- 注意定义和定义所在的文件(通常在表/列表/图的标题中给出)
在您的例子中,这种技术导致查找一个条目,其中指示此变量是Rails根据
routes.rb
文件的内容生成的路径。您应该检查该文件的内容,并使用命令行上的rake routes
查看当前定义的路由。在阅读Hartl教程时,您会遇到未定义的局部变量或方法
错误,或者希望了解变量或方法的当前定义在本教程中,以下技巧非常有用:
- 去网上看书
- 单击右上角的“查看为单页”链接
- 使用浏览器的搜索命令,从教程中的位置向后搜索有问题的变量/方法名称。(注意:如果有很多,并且您确定这是一个方法,您可以将搜索范围缩小到“def variable_或_method_in_question”)
- 注意定义和定义所在的文件(通常在表/列表/图的标题中给出)
在您的例子中,这种技术导致查找一个条目,其中指示此变量是Rails根据
routes.rb
文件的内容生成的路径。您应该检查该文件的内容,并使用命令行上的rake routes
查看当前定义的路由。这不再适用于Michael Hartl的教程
Spork现在没有在Rails 4的教程中使用,这可能会改变,我跟随了下面链接的页面,但它将覆盖现有的规范设置,并安装教程不需要的Gems
你在用Spork吗?如果是这样,则在routes.rb中进行更改时必须重新启动它
如清单3.38所述:
使用Spork时有一点建议:在更改预工作加载中包含的文件(如routes.rb)后,您必须重新启动Spork服务器以加载新的Rails环境。如果您的测试在您认为应该通过的时候失败了,请使用Ctrl-C退出Spork服务器并重新启动它
这一点不再适用于迈克尔·哈特尔的教程 Spork现在没有在Rails 4的教程中使用,这可能会改变,我跟随了下面链接的页面,但它将覆盖现有的规范设置,并安装教程不需要的Gems 你在用Spork吗?如果是这样,则在routes.rb中进行更改时必须重新启动它 如清单3.38所述: 使用Spork时有一点建议:在更改预工作加载中包含的文件(如routes.rb)后,您必须重新启动Spork服务器以加载新的Rails环境。如果您的测试在您认为应该通过的时候失败了,请使用Ctrl-C退出Spork服务器并重新启动它
对于Rails 5,向Rails\u helper.rb添加代码:
包括Rails.application.routes.url\u helpers
对于Rails 5,将代码添加到Rails\u helper.rb:包括Rails.application.routes.url\u helpers
添加您的spec\u helper.rb代码。并检查rake routes
输出是否已在上面发布route signup.spec_helper.rb。我似乎不能成功地运行rake路由;当我尝试时,我收到一个错误:“NameError:未定义的局部变量或main:Object的“routes”方法。”添加spec_helper.rb代码。并检查rake routes
输出是否已在上面发布route signup.spec_helper.rb。我似乎不能成功地运行rake路由;当我尝试时,我收到一个错误:“NameError:未定义的局部变量或main:Object的'routes'方法。”当我运行rake routes时,我得到的错误是:“NameError:未定义的局部变量或main:Object的'routes'方法。”好的,好消息是这解释了测试失败的原因。:-)停!我不明白。你在Hartl教程中最后一次成功的测试是什么?对不起。我已经没有主意了。我认为它真的应该包括那些助手,而不必明确地这样做。我发现了,但它似乎不适用于你。如果你知道了,请告诉我。再一次,很抱歉,我无法找到它的底部。当我运行rake路由时,我得到一个错误,它是这样的:“NameError:未定义的局部变量或main:Object的方法`routes.”好的,好的
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user@example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before {visit user_path(user)}
it {should have_content(user.name)}
it {should have_title(user.name)}
end
end