我是ROR的新手,我正在关注Hartl的ruby on rails教程。当我通过时,第6章的测试失败了:
$ bundle exec rspec spec /
以下测试失败:
Failures:
1) Static pages Home page should have the title 'Home'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/home"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top
(required)>'←[0m
2) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/home"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
3) Static pages Help page should have the title 'Help'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/help'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/help"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top
(required)>'←[0m
4) Static pages Help page should have the content 'Help'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/help'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/help"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:21:in `block (3 levels) in <top
(required)>'←[0m
5) Static pages About page should have the title 'About Us'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/about'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/about"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:39:in `block (3 levels) in <top
(required)>'←[0m
6) Static pages About page should have the content 'About Us'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/about'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/about"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:34:in `block (3 levels) in <top
(required)>'←[0m
7) UserPages signup page
←[31mFailure/Error:←[0m ←[31mit { should have_title(full_title('Sign up'))
}←[0m
←[31mNoMethodError←[0m:
←[31mundefined method `full_title' for #<RSpec::Core::ExampleGroup::Neste
d_3::Nested_1:0x4807bc0>←[0m
←[36m # ./spec/requests/user_pages_spec.rb:11:in `block (3 levels) in <top (
required)>'←[0m
8) UserPages signup page
←[31mFailure/Error:←[0m ←[31mit { should have_content('Sign up') }←[0m
←[31mexpected #has_content?("Sign up") to return true, got false←[0m
←[36m # ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (
required)>'←[0m
Finished in 0.70304 seconds
←[31m27 examples, 8 failures←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:12←[0m ←[36m# Static pages Home
page should have the title 'Home'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:25←[0m ←[36m# Static pages Help
page should have the title 'Help'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:20←[0m ←[36m# Static pages Help
page should have the content 'Help'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:38←[0m ←[36m# Static pages About
page should have the title 'About Us'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:33←[0m ←[36m# Static pages About
page should have the content 'About Us'←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:11←[0m ←[36m# UserPages signup pag
e ←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:10←[0m ←[36m# UserPages signup pag
e ←[0m
Randomized with seed 53867
以下是用户模型:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
这是RSpec模型/ user_spec.rb:
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { @user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
expect(@user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by(email: @user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
Routes.rb文件:
FirstApp::Application.routes.draw do
get "users/new"
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'
end
user_pages_spec.rb文件:
require 'spec_helper'
describe "UserPages" 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')) }
end
end
static_pages_spec.rb文件:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
end
答案 0 :(得分:2)
您已定义以下路线
users_new GET /users/new(.:format) users#new
root GET / static_pages#home
signup GET /signup(.:format) users#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
在static_pages_spec.rb
中,您访问的路由为visit '/static_pages/home'
,visit '/static_pages/help'
和visit '/static_pages/about'
,这显然会导致No route matches
错误,因为这些路线不会不存在(将它们与上面列出的路线匹配)。
您需要在static_pages_spec.rb
中进行以下更改:
错误:No route matches [GET] "/static_pages/home"
替换
visit '/static_pages/home'
用
visit root_path
或visit '/'
错误:No route matches [GET] "/static_pages/help"
替换
visit '/static_pages/help'
用
visit help_path
或visit '/help'
错误:No route matches [GET] "/static_pages/about"
替换
visit '/static_pages/about'
用
visit about_path
或visit '/about'
您在user_pages_spec.rb
文件中收到后两个错误:
错误:undefined method 'full_title'
它清楚地表明您使用的是一个名为full_title
的方法,而这个方法没有在任何地方定义。
it { should have_title(full_title('Sign up')) }
## ^
## full_title method here is undefined
错误:expected #has_content?("Sign up") to return true, got false
此错误仅表示以下示例失败
it { should have_content('Sign up') }
这意味着sign_up
视图没有文字注册。确保sign_up
视图的文字注册 完全,匹配确切的案例。