Unit Testing Custom Validation Method

L

leah

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 = "(e-mail address removed)"
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 = "(e-mail address removed)"
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.
 
L

LeahH

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?
 
L

LeahH

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top