2011-07-05 46 views
0

當創建新用戶時,會給他們一個密碼。如何在更新密碼時測試密碼的長度

我想測試用戶更新或更改其密碼的情況。

User.rb

validates :password, :on => :create, 
       :presence => true, 
       :confirmation => true, 
       :length => {:within => 6..12} 

before_validation :make_password, :on => :create 

在規格/型號/ user_spec.rb我有以下幾點:

形容 「密碼驗證」 之前做 (:每個)做 @user =廠( :用戶) 端

it "should reject passwords that are too long" do 
    too_long_password = "a" * 13 
    @user.update_attributes(@attr.merge(:password => too_long_password, :password_confirmation => too_long_password)).should_not be_valid 
end 

不起作用。 現在我該如何測試更新? 任何想法讚賞。

回答

2

從驗證中刪除:on => :create子句。刪除它將在創建和更新期間激活驗證。

validates :password,:presence => true, 
        :confirmation => true, 
        :length => {:within => 6..12} 

您沒有提及,但你也可以刪除:on => :create,如果需要在before_validation太

+0

我刪除了:上=>:創建按你的只會驗證指示,但需要它的before_validation與我分配密碼時一樣。我希望學習如何做的是得到如何編寫rspec測試來更新用戶信息。 – chell