現在、Rails チュートリアルの第 7 章を進めていますが、図 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/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