我目前正在阅读 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
我猜主要错误涉及未定义的方法或变量“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