Forum: RSpec be_valid (validates_format_of ..., :on => :create)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f46ce797c86a2bd7142ac40de29f6e3?d=identicon&s=25 Mateusz Winiarski (mawiniarski)
on 2009-03-02 00:08
(Received via mailing list)
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.
3880f04333bf8375b44cd6aa135406c7?d=identicon&s=25 Bart Zonneveld (Guest)
on 2009-03-02 15:54
(Received via mailing list)
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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-02 17:05
(Received via mailing list)
On Mon, Mar 2, 2009 at 8:20 AM, Bart Zonneveld <zuperinfinite@gmail.com>
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
3880f04333bf8375b44cd6aa135406c7?d=identicon&s=25 Bart Zonneveld (Guest)
on 2009-03-02 17:32
(Received via mailing list)
On 2-mrt-2009, at 16:50, David Chelimsky 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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-02 18:05
(Received via mailing list)
On Mon, Mar 2, 2009 at 10:23 AM, Bart Zonneveld
<zuperinfinite@gmail.com> 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-...,
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

Cheers,
David
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2009-03-02 18:14
(Received via mailing list)
On Mon, Mar 2, 2009 at 8:23 AM, Bart Zonneveld <zuperinfinite@gmail.com>
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
3880f04333bf8375b44cd6aa135406c7?d=identicon&s=25 Bart Zonneveld (Guest)
on 2009-03-03 10:57
(Received via mailing list)
On 2-mrt-2009, at 18:12, Mark Wilden 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
0f46ce797c86a2bd7142ac40de29f6e3?d=identicon&s=25 Mateusz Winiarski (mawiniarski)
on 2009-03-06 09:50
(Received via mailing list)
> FWIW, the OP's code says :on => :create. I also happened to 
seehttp://mawiniarski.wordpress.com/2009/02/28/rspec-...,
> which reinforced for me that this is about validating on create, not
> general validation.

I've figured that out after starting a topic.
This topic is locked and can not be replied to.