2012-02-26 52 views
1

我正在使用rspec,rails 3.2.1,並且在我的Devise「User」模型上運行測試。導軌沒有看到我的長度測試?

由於某些原因,我所有的測試都與長度無關(所有的測試都失敗了),但我已經進行了必要的驗證..另外,我的「必需」驗證工作正常。

作爲一個側面問題,我如何訂購我的驗證消息?用戶名錯誤消息在底部顯示,即使它的第一個輸入框中

class User < ActiveRecord::Base 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :username, :bio, :password, :password_confirmation, :remember_me 

    username_regex = /\A[\w_]+\z/i 

    validates :username, :presence => true, 
         :uniqueness => true, 
         :format  => { :with => username_regex, 
             :message => "Username can only contain letters, numbers and underscores." }, 
         :length  => { :minimum => 3, 
             :maximum => 15 } 

    validates :bio,  :length  => { :maximum => 255 } 

end 

這裏是我的測試,是失敗

require 'spec_helper' 

describe User do 

    before(:each) do 
    @attr = { 
     :username => "TestUser", 
     :email => "[email protected]", 
     :password => "123456", 
     :bio  => "This is my bio" 
    } 
    end 

    it "should create a new instance given a valid attribute" do 
    User.create!(@attr) 
    end 

    it "should require a username" do 
    no_username_user = User.new(@attr.merge(:username => "")) 
    no_username_user.should_not be_valid 
    end 

    it "should require an email" do 
    no_email_user = User.new(@attr.merge(:email => "")) 
    no_email_user.should_not be_valid 
    end 

    it "should reject spaces in username" do 
    space_username = User.new(@attr.merge(:username => "Test User")) 
    space_username.should_not be_valid 
    end 

    it "should reject usernames that are too long" do 
    long_username = User.new(@attr.merge(:username => "#{'a'*16}")) 
    long_username.should_not be_valid 
    end 

    it "should reject short bios" do 
    long_bio = User.new(@attr.merge(:bio => "12")) 
    long_bio.should_not be_valid 
    end 

    it "should reject long bios" do 
    long_bio = User.new(@attr.merge(:bio => "#{'b'*256}")) 
    long_bio.should_not be_valid 
    end 

    it "should allow no bio" do 
    no_bio = User.new(@attr.merge(:bio => "")) 
    no_bio.should be_valid 
    end 
end 
+0

那麼哪些規格正在失敗? – 2012-02-26 06:15:25

+2

您是否嘗試過重新啓動服務器?另外,當您嘗試在控制檯中創建用戶而未進行驗證時會發生什麼情況?調用user.errors時是否顯示您的長度驗證? – PhillipKregg 2012-02-26 06:18:50

+0

我是個白癡。重新啓動服務器解決了問題:x謝謝Phillip – Tallboy 2012-02-26 06:21:33

回答

0

這可能是因爲你的服務器緩存的代碼,嘗試重新啓動它。

+0

重複,看到評論 – phoet 2012-02-26 11:26:31

+0

哦,我不知道 – 2012-02-26 12:16:32