2014-06-13 69 views
1

我遵循Rails教程建模用戶章節:http://www.railstutorial.org/book/modeling_users#cha-modeling_usersRails教程驗證問題

我user.rb樣子:

class User < ActiveRecord::Base 
    before_save { self.email = email.downcase } 
    has_secure_password 
    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i 
    validates :email, presence: true, 
       format:  { with: VALID_EMAIL_REGEX }, 
       uniqueness: { case_sensitive: false } 

    validates :password, length: { minimum: 6 } 
end 

和我的用戶模型規格的樣子:

describe User, :type => :model 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 } 

    ... (other methods are here) 

describe "when password is not present" do 
    before do 
    @user = User.new(name: "Example User", email: "[email protected]", 
        password: "foobar", password_confirmation: "foobar") 
    end 
    it { should_not be_valid } 
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 

這一點我敢肯定是完全的複製Rails的教程代碼是什麼,但我得到以下失敗的測試錯誤:

rspec ./spec/models/user_spec.rb:83 # User when password is not present should not be valid 
rspec ./spec/models/user_spec.rb:108 # User return value of authenticate method with invalid password should be false 
+0

你能在問題補充完整的錯誤跟蹤解決第二個。 –

+0

@infinity它也考慮到了,請檢查測試數據庫中是否還有用戶的電子郵件地址爲「[email protected]」。從命令行:1. rails console test 2. user = User.find_by_email(「[email protected]」)3.如果上一個命令找到用戶,則需要刪除它:user.destroy或者,如果用戶存在於數據庫中,您可能需要考慮每次使用新的電子郵件地址(電子郵件地址中的某些隨機部分)在測試中創建用戶。 – jyrkim

回答

2

我檢查了這一點通過查看Ruby on Rails教程手冊的源代碼(Rails 4)在GitHub上:spec/models/user_spec.rb。根據那裏的代碼,它看起來像你的密碼目前是可接受的類型,這就是爲什麼你的測試失敗。我的意思是你的密碼很好。 foob​​ar是一個有效的密碼。在空字符串下面傳遞給用戶模型驗證。

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 

第二個我不知道,但它會幫助,如果你嘗試了Rails 3 spec/models/user_spec.rb相關的代碼相同的測試:

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 

它看起來略有不同,但它的測試相同事情。這只是建議,因爲我不確定發生了什麼問題。

0

它看起來像缺少一些方法(我F你正在努力教程完全匹配)清單6.25 & 6.28:前{@ user.password_confirmation = 「不匹配」} 它{should_not be_valid}

形容 「當密碼不匹配確認」 做 結束

形容 「用密碼太短」 之前做 {@ user.password的= @ user.password_confirmation = 「一個」 * 5} 它應該{} be_invalid結束

+0

@indescrete,我在我的規範中有這些方法,我只是沒有將它們添加到問題中,因爲我不認爲它們會影響失敗的測試 – infinity

+0

您是完全正確的。我沒有注意到...上面的代碼。在「密碼不存在時」的方法中,您使用的是foobar,但我相信您會希望這些字段爲空字符串。請參見清單6.25:描述「密碼不存在時」 在做之前 @user = User.new(name:「示例用戶」,電子郵件:「[email protected]」, password:「」,password_confirmation:「 「) end it {should_not be_valid} end – indiscrete

+0

listing 6。25應該給你失敗的測試,但是,根據教程(除非你已經過了清單6.25並修復了失敗的測試) – indiscrete

1

我有同樣的問題,我通過改變be_false是假的(不帶下劃線)