views:

19

answers:

1

Hey everybody,

I am trying to write a unit testing for a User model in Ruby on Rails. I am using authlogic and need to check that the first_name and last_name of the user model attributes are not the same when the user is registering.

This is my user model:

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

As you can see, I tried to create a custom validator creating a new file in /lib/different_names_validator.rb with a class called DifferntNamesValidator, but couldn't get it, as I got the following error: Unknown validator: 'different_names' (ArgumentError)

Thanks in advance!

+1  A: 

Hi Try to include this module in your model

Bohdan Pohorilets
Which module? How can I do it?
noloman
I suppose that inside different_names_validator.rb you defined module(or class) DifferentNamesValidator so try to add 'include DifferentNamesValidator' inside your model
Bohdan Pohorilets
http://stackoverflow.com/questions/1073076/rails-lib-modules-and this one may be helpful
Bohdan Pohorilets
Uh ok, thanks a lot!
noloman