[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unit Testing Custom Validation Method

leah

6/21/2007 8:38:00 PM

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 = "someone@example.com"
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 = "someone@example.com"
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.

2 Answers

leah

6/22/2007 5:25:00 PM

0

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?

leah

6/24/2007 11:05:00 PM

0

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!