Forum: Ruby Unit Testing Custom Validation Method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2007-06-22 00:41
(Received via mailing list)
Hey there

I've written a custom validation method (to validate an email
address).  It looks like

module ActiveRecord
  module Validations
    module ClassMethods

      def validates_as_email
        ...
      end

    end
  end
end

I've put this code in app/lib/custom_validations.rb

Now, I'm trying to write a unit test but I'm running into problems.
One attempt at a unit test looks like

require File.dirname(__FILE__) + '/../test_helper'
require 'lib/custom_validations'

class CustomValidationsTest < Test::Unit::TestCase

  def test_validates_as_email
    email = "removed_email_address@domain.invalid"
    assert validates_as_email( email )
  end
end

This throws a
NoMethodError: undefined method `validates_as_email' for
#<CustomValidationsTest:0x314bd80>
It looks to me like the test is looking for the validates_as_email
method to be defined in CustomValidationsTest rather than finding it
in lib/custom_validations.rb


So, I tried this

require File.dirname(__FILE__) + '/../test_helper'
require 'lib/custom_validations'

class CustomValidationsTest < Test::Unit::TestCase

  def test_validates_as_email
    email = "removed_email_address@domain.invalid"
    assert
ActiveRecord::Validations::ClassMethods.validates_as_email( email )
  end
end

And I got this error
NoMethodError: undefined method `validates_as_email' for
ActiveRecord::Validations::ClassMethods:Module

What am I missing?  I'll bet it's something simple, but I just can't
figure out what's wrong.  Thanks for the help.
LeahH (Guest)
on 2007-06-22 21:27
(Received via mailing list)
I've also tried adding this

require File.dirname(__FILE__) + '/../test_helper'
require 'lib/custom_validations'

class CustomValidationsTest < Test::Unit::TestCase

  include ActiveRecord::Validations
  ...
end

which results in this error
`alias_method': undefined method `save' for class
`CustomValidationsTest'

So, then I tried this

require File.dirname(__FILE__) + '/../test_helper'
require 'lib/custom_validations'

class CustomValidationsTest < Test::Unit::TestCase

  include ActiveRecord::Validations
  def save
    ...
  end
  ...
end

and this

require File.dirname(__FILE__) + '/../test_helper'
require 'lib/custom_validations'

class CustomValidationsTest < Test::Unit::TestCase

  include ActiveRecord::Validations
  def self.save
    ...
  end
  ...
end

both of which resulted in the same error given above.

Does anyone have any ideas?
LeahH (Guest)
on 2007-06-25 03:06
(Received via mailing list)
Alrighty, I have a solution...

Instead of embedding all my validation code in custom_validations,
I've moved the code into it's own module and I call it from
custom_validations like this

lib/custom_validations.rb
require 'lib/utils/authentication'

module ActiveRecord
  module Validations
    module ClassMethods

      def validates_as_email(*attr_names)
        configuration = { :message => "Invalid email address" }
        configuration.update(attr_names.pop) if attr_names.last.is_a?
(Hash)

        validates_each(attr_names, configuration) do |record,
attr_name, value|
          record.errors.add(attr_name, configuration[:message])
unless !value.nil? and Utils::Authentication.valid_email?( value )
        end
      end
    end
  end
end


lib/utils/authenticaion.rb
module Utils
  module Authentication

    def valid_email?( value )
      ... a whole lot of regexp code ...
    end

  end
end

Now I can unit test my custom validation code.
Sweet!
This topic is locked and can not be replied to.