Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shoulda validate_format_of . not_with has problem in framework (or in my understanding)

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('[email protected]')}

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

like image 790
user426623 Avatar asked Nov 09 '10 13:11

user426623


People also ask

Why is problem validation important?

Regardless of the type of person you are, you're missing a fundamental step prior to scoping and developing your proposed solution: Problem validation! There is a difference between having an idea and validating an idea. Validation is the first step in moving towards learning more about the problem you are ultimately looking to solve.

Why do I get a template validation error when using bucket?

This returns the following error: "Template validation error: Invalid template property or properties [Bucket]." The error is caused because the CloudFormation template validator sees the bucket resource as a section-level specification, which isn't allowed as a template property.

What is the difference between having an idea and validating it?

There is a difference between having an idea and validating an idea. Validation is the first step in moving towards learning more about the problem you are ultimately looking to solve. Too many times people are quick to hypothesise a solution with little understanding of the problem.

How to validate required annotation in Entity Framework?

But Entity Framework will also recognize the Requiredannotation and validate it. A simple way to test this is to disable MVC's client-side validation feature. You can do this in the MVC application's web.config file. The appSettings section has a key for ClientValidationEnabled.


2 Answers

Try this instead:

it { should_not allow_value("test@test").for(:email) }
like image 194
zetetic Avatar answered Oct 02 '22 19:10

zetetic


I just ran into a similar problem Turns out you need to invoke the with_message method and supply the exact error message as a string, or a regex that matches the error message. Doing so will convince the validate_format_of to cease its stubborn insistence that format errors result in "can't be blank" messages, and actually pass. For example:

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

becomes

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

This sure looks like a bug in the shoulda library.

like image 43
thatothermitch Avatar answered Oct 02 '22 18:10

thatothermitch