我目前正在研究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 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
我猜主要的错误涉及未定义的方法或varialble“signup_path”,但我不知道它应该被定义的位置,或者它是否应该首先由Rails自动定义。
有人可以帮我这个吗?
更新:以下是我的路线档案。
感谢您的回复。这是我的路线文件。除非我遗漏了什么,否则一切似乎都没问题:
SecondSampleApp::Application.routes.draw do
get "users/new"
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
resources :users
root "static_pages#home"
match "/signup", to: "users#new", via: "get"
match "/help", to: "static_pages#help", via: "get"
match "/about", to: "static_pages#about", via: "get"
match "/contact", to: "static_pages#contact", via: "get"
end
规格/ 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
答案 0 :(得分:2)
在浏览Hartl教程时遇到undefined local variable or method
错误或者想要找出教程中该变量或方法的当前定义应该是什么时,以下技术是有帮助的:
有时也可以使用github存储库,但这会显示代码的最终状态/位置,这通常会产生误导。
在您的情况下,此技术会导致在Table 5.1中找到一个条目,该条目表明此变量是由于routes.rb
文件的内容而由Rails生成的路径。您应该检查该文件的内容,并在命令行上使用rake routes
查看当前定义的路由。
答案 1 :(得分:2)
此不再适用于Michael Hartl http://www.railstutorial.org/book
的教程Spork现在没有在Rails 4的教程中使用,这可能会改变,我按照下面链接的页面,但它将覆盖现有的规范设置并安装本教程不需要的Gems。
你在使用Spork吗?如果是这样,只要在routes.rb。
中进行更改,就必须重新启动它如清单3.38所述:
使用Spork时的一个建议:在更改prefork加载中包含的文件(例如routes.rb)之后,您将不得不重新启动Spork服务器以加载新的Rails环境。如果您认为应该通过测试失败,请使用Ctrl-C退出Spork服务器并重新启动它。
http://ruby.railstutorial.org/chapters/static-pages#sec-spork
答案 2 :(得分:0)
对于Rails 5,将代码添加到rails_helper.rb:
include Rails.application.routes.url_helpers