2014-06-09 146 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)並按照指導原則實施該方法。而對於第二個錯誤,我已經明確指出我的答案應該做什麼。 –

相關問題