# Unit Testing Custom Validation Method

**URL:** <https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107451>\
**Category:** Ruby\
**Created:** [June 21, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107451 "2007-06-21T22:41:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 21, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107451/1 "2007-06-21T22:41:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 22, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107451/2 "2007-06-22T19:27:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 25, 2007, 1:06am UTC](https://www.ruby-forum.com/t/unit-testing-custom-validation-method/107451/3 "2007-06-25T01:06:11Z")

</div>

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!
