2013-03-08 79 views
1

我得到這些失敗,我不知道在哪裏尋找解決問題。我搜索了這個網站,找不到有我的問題的人。根據Hartl教程,所有的測試都應該通過。在Hartls rails教程ch上獲取Rspec測試失敗。 6

的故障:

C:\Sites\rails_projects\sample_app>bundle exec rspec spec 
Rack::File headers parameter replaces cache_control after Rack 1.5. 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m 
←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m←[32m.←[0m 
←[32m.←[0m←[31mF←[0m←[31mF←[0m←[31mF←[0m←[32m.←[0m←[32m.←[0m 

Failures: 

    1) when email address is already taken 
    ←[31mFailure/Error:←[0m ←[31muser_with_same_email = @user.dup←[0m 
    ←[31mTypeError:←[0m 
     ←[31mcan't dup NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:104:in `dup'←[0m 
←[36m  # ./spec/models/user_spec.rb:104:in `block (2 levels) in <top (require 
d)>'←[0m 

    2) when name is too long 
    ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = "a" * 51 }←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `name=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:76:in `block (2 levels) in <top (required 
)>'←[0m 

    3) when password is not present 
    ←[31mFailure/Error:←[0m ←[31mbefore { @user.password = @user.password_confi 
rmation = " " }←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:55:in `block (2 levels) in <top (required 
)>'←[0m 

    4) when password confirmation is nil 
    ←[31mFailure/Error:←[0m ←[31mbefore { @user.password_confirmation = nil }←[ 
0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:65:in `block (2 levels) in <top (required 
)>'←[0m 

    5) when email format is valid should be valid 
    ←[31mFailure/Error:←[0m ←[[email protected] = valid_address←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `email=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:97:in `block (3 levels) in <top (required 
)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:96:in `each'←[0m 
←[36m  # ./spec/models/user_spec.rb:96:in `block (2 levels) in <top (required 
)>'←[0m 

    6) when password doesn't match confirmation 
    ←[31mFailure/Error:←[0m ←[31mbefore {@user.password_confirmation = "mismatc 
h" }←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `password_confirmation=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required 
)>'←[0m 

    7) when name is not present 
    ←[31mFailure/Error:←[0m ←[31mbefore { @user.name = " " }←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `name=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:71:in `block (2 levels) in <top (required 
)>'←[0m 

    8) when email format is invalid should be invalid 
    ←[31mFailure/Error:←[0m ←[[email protected] = invalid_address←[0m 
    ←[31mNoMethodError:←[0m 
     ←[31mundefined method `email=' for nil:NilClass←[0m 
←[36m  # ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required 
)>'←[0m 
←[36m  # ./spec/models/user_spec.rb:86:in `each'←[0m 
←[36m  # ./spec/models/user_spec.rb:86:in `block (2 levels) in <top (required 
)>'←[0m 

Finished in 2.42 seconds 
←[31m30 examples, 8 failures←[0m 

Failed examples: 

←[31mrspec ./spec/models/user_spec.rb:109←[0m ←[36m# when email address is alrea 
dy taken ←[0m 
←[31mrspec ./spec/models/user_spec.rb:77←[0m ←[36m# when name is too long ←[0m 
←[31mrspec ./spec/models/user_spec.rb:56←[0m ←[36m# when password is not present 
←[0m 
←[31mrspec ./spec/models/user_spec.rb:66←[0m ←[36m# when password confirmation i 
s nil ←[0m 
←[31mrspec ./spec/models/user_spec.rb:94←[0m ←[36m# when email format is valid s 
hould be valid←[0m 
←[31mrspec ./spec/models/user_spec.rb:61←[0m ←[36m# when password doesn't match 
confirmation ←[0m 
←[31mrspec ./spec/models/user_spec.rb:72←[0m ←[36m# when name is not present ←[0 
m 
←[31mrspec ./spec/models/user_spec.rb:82←[0m ←[36m# when email format is invalid 
should be invalid←[0m 

我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 "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 == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
    end 
end 

    describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " " } 
    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 "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
    end 


    describe "when name is not present" do 
    before { @user.name = " " } 
    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 
     @user.should_not 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 
     @user.should 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 
    end 

User.rb文件:

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.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 } 
    validates :password, presence: true, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 
end 

有什麼建議?

回答

1

這可能是因爲您似乎在describe "with invalid password"之後不小心關閉了您的describe User區塊,並有兩個額外的end s。

目前:

describe User do 

    # ... 
    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
    end 
end 

# ... 

end 

應該是:

describe User do 

    # ... 
    describe "with invalid password" do 
    let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

    it { should_not == user_for_invalid_password } 
    specify { user_for_invalid_password.should be_false }  
    end 

    # ... 
end 

此外,檢查您的壓痕和end的號碼對文件的末尾。

+0

我認爲保羅是對的,那也是我發現的一個錯誤。 – PericlesTheo 2013-03-09 15:40:37

+0

感謝Paul和@PericlesThedorou。我不得不看哈特爾Github找出我在語法方面缺少的東西。 – Tasbent 2013-03-11 10:24:38

0

錯誤消息告訴我們@user永遠不會被實例化

你好像缺少before

before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
     password: "foobar", password_confirmation: "foobar") 
end 

:each應該是:

before (:each) do 
    @user = User.new(name: "Example User", email: "[email protected]", 
     password: "foobar", password_confirmation: "foobar") 
end 

的@現在應該使用戶對象&測試變爲綠色:)

+0

感謝您的回覆,但沒有奏效。我仍然得到相同的錯誤。 ':每個'不是教程的一部分,所以我不認爲這是這裏的問題。 – Tasbent 2013-03-09 03:16:44

相關問題