1

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!

shingara
  • 46,608
  • 11
  • 99
  • 105
noloman
  • 11,411
  • 20
  • 82
  • 129

1 Answers1

1

Hi Try to include this module in your model

Bohdan
  • 8,298
  • 6
  • 41
  • 51
  • Which module? How can I do it? – noloman Oct 13 '10 at 10:33
  • 1
    I suppose that inside different_names_validator.rb you defined module(or class) DifferentNamesValidator so try to add 'include DifferentNamesValidator' inside your model – Bohdan Oct 13 '10 at 10:39
  • 1
    http://stackoverflow.com/questions/1073076/rails-lib-modules-and this one may be helpful – Bohdan Oct 13 '10 at 10:46