views:

46

answers:

1

Hi,

I'm wondering how to write a cucumber feature and spec to check the following validation

Field email, here is the validation in the model

validates :email, :presence => true, 
                  :length => {:minimum => 3, :maximum => 254},
                  :uniqueness => true,
                  :format => {:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i}

Thanks

+2  A: 

Here's my opinion on what you're doing, take it or leave it.

1) You should be using Test::Unit or Rspec to test model behavior such as validations. Other tools like Shoulda can also provide additional confidence.

2) Use the validation helper methods such as validates_presence_of :email (http://api.rubyonrails.org/classes/ActiveModel/Validations/HelperMethods.html). It will make your code easier to read and more in line with standard rails convention.

3) Don't reinvent the wheel. Tools like authlogic already take care of email validation. Even if you're not validating a user sign up, you can still use their email regex like:

validates_format_of :email, :with => Authlogic::Regex.email
Beerlington
another one is http://github.com/alexdunae/validates_email_format_of
Nadal