2010-10-13 63 views
1

我想在Ruby on Rails中爲用戶模型編寫一個單元測試。我正在使用authlogic,並且需要檢查用戶註冊時用戶模型屬性的first_name和last_name是否不相同。驗證屬性的內容不一樣

這是我的用戶模型:

class User < ActiveRecord::Base 
    acts_as_authentic do |c| 
    c.login_field= :username 
    end 
has_many :memberships, :class_name => "Project::Membership" 
    has_many :projects, :through => :memberships 
    has_one :profile 

    validates :email, :presence => true, :uniqueness => true 
    validates :username, :presence => true, :uniqueness => true 
    validates :first_name,:presence => true 
    validates:last_name, :presence => true 
    validates :title, :presence => true 
    validates :password, :presence => true 
    validates :password_confirmation, :presence => true 
    validates :gender, :presence => true 
    # Custom validator 
    validates :first_name, :last_name, :different_names => true 

正如你所看到的,我試圖創建一個自定義驗證開創了一個名爲DifferntNamesValidator類/lib/different_names_validator.rb一個新的文件,但不能得到它,因爲我得到以下錯誤:未知的驗證:'different_names'(ArgumentError)

在此先感謝!

回答

1

嗨嘗試在您的模型中包含此模塊

+0

哪個模塊?我該怎麼做? – noloman 2010-10-13 10:33:01

+1

我想在你的模塊(或類)DifferentNamesValidator中定義的different_names_validator.rb裏面,所以試着在你的模型中添加'include DifferentNamesValidator' – Bohdan 2010-10-13 10:39:33

+1

http://stackoverflow.com/questions/1073076/rails-lib-modules-這個可能有幫助 – Bohdan 2010-10-13 10:46:35