Ruby – Shoulda validate_format_of . not_with has problem in framework (or in the understanding)

rspecrubyruby-on-rails-3shoulda

I put the following code into an RSpec test:

it { should validate_format_of(:email).not_with('test@test')}

and setup the actual class with:

validates :email, :presence => true, :format => /\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i

And when I run the tests I get:

Failures:
1) User
Failure/Error: it { should validate_format_of(:email).not_with('test@test')}
Expected errors to include "can't be blank" when email is set to "test@test", got errors: ["name can't be blank (nil)", "email is invalid (\"test@test\")"]
# ./spec/models/user_spec.rb:8:in `block (2 levels) in '

When I do a passing test like:

it { should validate_format_of(:email).with('adam@trimediatlantic.com')}

Everything works as expected. Can someone tell me if I'm doing something wrong or if this is a framework problem. Thank you.

Best Answer

Try this instead:

it { should_not allow_value("test@test").for(:email) }
Related Topic