Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email validation in user model #7

Open
walteryu opened this issue Jul 22, 2011 · 1 comment
Open

Email validation in user model #7

walteryu opened this issue Jul 22, 2011 · 1 comment

Comments

@walteryu
Copy link

I noticed one failing test in user_spec.rb when running 'rspec spec' for the first time:

Failures:

  1. User should reject invalid email addresses
    Failure/Error: invalid_email_user.should_not be_valid
    expected valid? to return false, got true

    ./spec/models/user_spec.rb:35:in `block (3 levels) in <top (required)>'

    ./spec/models/user_spec.rb:33:in`each'

    ./spec/models/user_spec.rb:33:in `block (2 levels) in <top (required)>'

Finished in 2.11 seconds
17 examples, 1 failure

I got the test to pass after adding email validation to the user model using an example from ASCIICasts found here: http://asciicasts.com/episodes/211-validations-in-rails-3:

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

After which, the test passes:

.................

Finished in 1.87 seconds
17 examples, 0 failures

Not sure if anyone else ran into this issue but wanted to share.

Thanks,
Walter

@manewitz
Copy link

When I plugged in the user model validation you used, I still had the same issue. I ended up using the factory_girl gem and setting up a User factory instead of the @attr hash. The spec passed once I changed it to:

it "should reject invalid email addresses" do
    addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
    addresses.each do |address|

        #original line
        #invalid_email_user = User.new(@attr.merge(:email => address))

        # changed to
        invalid_email_user = Factory.build(:user, :email => address)

        invalid_email_user.should_not be_valid
    end
end

I've yet to figure out why it wouldn't work the first way, the code should be doing the same thing functionally, but it wasn't catching the validation. There's a config setting for devise that lets you change the regex that validates the email address, but I've had no luck so far getting it to work with Rspec testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants