# Unit Testing Custom Validation Method

**URL:** <https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107466>\
**Category:** Rails\
**Created:** [June 22, 2007, 12:52am UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107466 "2007-06-22T00:52:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeahH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b4bc9f/32.png) [@LeahH](https://www.ruby-forum.com/u/LeahH)\
**Post date:** [June 22, 2007, 12:52am UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107466/1 "2007-06-22T00:52:00Z")

</div>

I posted this on comp.lang.ruby and thought it would be in my best  
interest to post it here too.

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.
