2014-06-09 92 views
0

我是ROR中的新成员,我遵循Hartl的rails教程ruby。第6章的测试失败了,当我路过:Ruby on Rails教程第6章RSpec测试失败,不知道为什么

$包的exec rspec的投机/

下面的测试失败:

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+\-.][email protected][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: "[email protected]", 
      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[[email protected],com user_at_foo.org [email protected] 
        [email protected]_baz.com [email protected]+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[[email protected] [email protected] [email protected] [email protected]] 
     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: "[email protected]", 
         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 
+2

'routes.rb'文件? –

+0

所有错误都是'user_pages_spec.rb'和'static_pages_spec.rb',但不是在此测试代码中,您提供了。因此给我们这2个文件.. –

+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以下更改:

  1. 错误No route matches [GET] "/static_pages/home"

    更换

    visit '/static_pages/home' 
    

    随着

    要么visit root_pathvisit '/'

  2. 错误No route matches [GET] "/static_pages/help"

    替换

    visit '/static_pages/help' 
    

    随着

    要么visit help_pathvisit '/help'

  3. 错误No route matches [GET] "/static_pages/about"

    更换

    visit '/static_pages/about' 
    

    随着

    要么visit about_pathvisit '/about'

您在user_pages_spec.rb文件接收下一个2个错误:

  1. 错误undefined method 'full_title'

    它清楚地给出了你使用一个名为full_title方法,不被任何定义的线索。

    it { should have_title(full_title('Sign up')) } 
             ##^
             ## full_title method here is undefined 
    
  2. 错误expected #has_content?("Sign up") to return true, got false

    此错误只是意味着下面的例子是失败

    it { should have_content('Sign up') } 
    

    这意味着sign_up视图没有文字注册。确保sign_up视图有文本注册正好,以及确切的案例以及。

+0

嘿,如何纠正这最后两个错误,请告诉我该怎么做这些改变以及我应该做些什么改变。由于我是ROR新手,如果您告诉我我应该如何处理这些错误,将会有所帮助。 –

+0

@MukeshThawani检查[Michael Hartl:full_title方法](http://www.railstutorial.org/book/rails_flavored_ruby#code-title_helper)并按照指导原则实施该方法。而对于第二个错误,我已经明确指出我的答案应该做什么。 –

相关问题