# Be\_valid (validates\_format\_of ..., :on =\> :create)

**URL:** <https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744>\
**Category:** RSpec\
**Created:** [March 2, 2009, 12:08am UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744 "2009-03-02T00:08:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 12:08am UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/1 "2009-03-02T00:08:57Z")

</div>

Greetings,

How to write Example which will check if model’s variable’s  
format is valid using :on =\> :create, like this:

class User \< ActiveRecord::Base  
…  
validates\_format\_of :email, :with =\> /…/, :on =\> :create  
…

Using following code is not right:  
it “should …” do  
@user = users(:example\_user)  
@user.email = ‘invalid\_email\_format’  
@user.save  
@user.should\_not be\_valid  
end

Even those code is not right:  
it "should … " do  
@user = users(:example\_user)  
@user.email = ‘invalid\_email\_format’  
@user.save  
@user.should be\_valid  
end

Thanks.

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/2 "2009-03-02T15:54:02Z")

</div>

On 28-feb-2009, at 11:45, MAwiniarski wrote:

> Using following code is not right:  
> it “should …” do  
> @user = users(:example\_user)  
> @user.email = ‘invalid\_email\_format’  
> @user.save  
> @user.should\_not be\_valid  
> end

Try:

it “should …” do  
user = User.new # create a NEW user, instead of loading an  
already saved user from a fixtures file  
user.email = ‘invalid\_email\_format’  
user.should\_not be\_valid  
user.should have(1).errors\_on(:email)  
end

cheers,  
bartz

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/3 "2009-03-02T17:32:49Z")

</div>

On 2-mrt-2009, at 16:50, David C. wrote:

> > > class User \< ActiveRecord::Base  
> > > end  
> > > end
> 
> +1
> 
> I might combine the first two lines:
> 
> user = User.create(:email =\> “invalid\_email\_format”)
> 
> That reads more clearly to me because the invalid email format is  
> assigned on create, not after. It would have an extra call to valid?  
> but I think it’s worth it for the clarity of intent in this case.

Although I agree with the reasoning you display here, I’d _never_  
validate any attribute just on create.  
I’m pretty sure a user can update his email address somewhere in the  
site, but then his email address wouldn’t be validated anymore.

On a second note, I noticed rspec default generated model specs now  
use Model.create!(@valid\_attributes) as their default “all is valid”  
test. What’s the advantage of this approach? I just write  
@model.attributes = @valid\_attributes; @model.should be\_valid, to  
prevent these specs to actually hit the db, and therefore speed up my  
specs a bit.

thanks,  
bartz

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/4 "2009-03-02T18:05:18Z")

</div>

On Mon, Mar 2, 2009 at 10:23 AM, Bart Z.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > How to write Example which will check if model’s variable’s  
> > > > @user.email = ‘invalid\_email\_format’  
> > > > user.should\_not be\_valid  
> > > > assigned on create, not after. It would have an extra call to valid?  
> > > > but I think it’s worth it for the clarity of intent in this case.
> 
> Although I agree with the reasoning you display here, I’d _never_ validate  
> any attribute just on create.  
> I’m pretty sure a user can update his email address somewhere in the site,  
> but then his email address wouldn’t be validated anymore.

FWIW, the OP’s code says :on =\> :create. I also happened to see  
[http://mawiniarski.wordpress.com/2009/02/28/rspec-validation-on-create/](http://mawiniarski.wordpress.com/2009/02/28/rspec-validation-on-create/),  
which reinforced for me that this is about validating on create, not  
general validation.

> On a second note, I noticed rspec default generated model specs now use  
> Model.create!(@valid\_attributes) as their default “all is valid” test.  
> What’s the advantage of this approach? I just write @model.attributes =  
> @valid\_attributes; @model.should be\_valid, to prevent these specs to  
> actually hit the db, and therefore speed up my specs a bit.

Good point. Wanna submit a patch? [http://rspec.lighthouseapp.com](http://rspec.lighthouseapp.com)

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/5 "2009-03-02T17:05:32Z")

</div>

On Mon, Mar 2, 2009 at 8:20 AM, Bart Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > validates\_format\_of :email, :with =\> /…/, :on =\> :create  
> > Try:
> 
> it “should …” do  
> user = User.new # create a NEW user, instead of loading an already saved  
> user from a fixtures file  
> user.email = ‘invalid\_email\_format’  
> user.should\_not be\_valid  
> user.should have(1).errors\_on(:email)  
> end

+1

I might combine the first two lines:

user = User.create(:email =\> “invalid\_email\_format”)

That reads more clearly to me because the invalid email format is  
assigned on create, not after. It would have an extra call to valid?  
but I think it’s worth it for the clarity of intent in this case.

Otherwise, this is the right idea, IMO.

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 2, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/6 "2009-03-02T18:14:08Z")

</div>

On Mon, Mar 2, 2009 at 8:23 AM, Bart Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On a second note, I noticed rspec default generated model specs now use  
> Model.create!(@valid\_attributes) as their default “all is valid” test.  
> What’s the advantage of this approach? I just write @model.attributes =  
> @valid\_attributes; @model.should be\_valid, to prevent these specs to  
> actually hit the db, and therefore speed up my specs a bit.

Wouldn’t it be better to go through the complete create! cycle, to  
make sure callbacks and database constraints (if any) are exercised?

///ark

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 3, 2009, 10:57am UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/7 "2009-03-03T10:57:48Z")

</div>

On 2-mrt-2009, at 18:12, Mark W. wrote:

> > actually hit the db, and therefore speed up my specs a bit.
> 
> Wouldn’t it be better to go through the complete create! cycle, to  
> make sure callbacks and database constraints (if any) are exercised?

Could be, but imagine I have 5 validations on my model. Only the  
first will throw an error on create!.  
And since my validations are in Dutch, I specifically test each  
validation to see whether it returns the correct error message.

I use the following structure:

class Article  
validates\_presence\_of :title  
end

describe Article  
before(:each) do  
@article = Article.new  
@valid\_attributes = { :title =\> “Title” }  
end

it “should not be valid without a title” do  
@article.attributes = @valid\_attributes.except :title  
@article.should\_not be\_valid  
end

it “should be valid with all valid attributes” do  
@article.attributes = @valid\_attributes  
@article.should be\_valid  
end  
end

---

<div class="post-metadata">

**Author:** ![mawiniarski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/fbc32d/32.png) [@mawiniarski](https://www.ruby-forum.com/u/mawiniarski)\
**Post date:** [March 6, 2009, 9:50am UTC](https://www.ruby-forum.com/t/be-valid-validates-format-of-on-create/161744/8 "2009-03-06T09:50:30Z")

</div>

> FWIW, the OP’s code says :on =\> :create. I also happened to seehttp://mawiniarski.wordpress.com/2009/02/28/rspec-validation-on-create/,  
> which reinforced for me that this is about validating on create, not  
> general validation.

I’ve figured that out after starting a topic.
