MAwiniarski
2009-Feb-28 10:45 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
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.
Bart Zonneveld
2009-Mar-02 14:20 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On 28-feb-2009, at 11:45, MAwiniarski wrote:> 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 > endTry: 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
David Chelimsky
2009-Mar-02 15:50 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On Mon, Mar 2, 2009 at 8:20 AM, Bart Zonneveld <zuperinfinite at gmail.com> wrote:> > On 28-feb-2009, at 11:45, MAwiniarski wrote: > >> 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 > > 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> > cheers, > bartz > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Bart Zonneveld
2009-Mar-02 16:23 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On 2-mrt-2009, at 16:50, David Chelimsky wrote:> On Mon, Mar 2, 2009 at 8:20 AM, Bart Zonneveld > <zuperinfinite at gmail.com> wrote: >> >> On 28-feb-2009, at 11:45, MAwiniarski wrote: >> >>> 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 >> >> 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.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
David Chelimsky
2009-Mar-02 16:38 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On Mon, Mar 2, 2009 at 10:23 AM, Bart Zonneveld <zuperinfinite at gmail.com> wrote:> > On 2-mrt-2009, at 16:50, David Chelimsky wrote: > >> On Mon, Mar 2, 2009 at 8:20 AM, Bart Zonneveld <zuperinfinite at gmail.com> >> wrote: >>> >>> On 28-feb-2009, at 11:45, MAwiniarski wrote: >>> >>>> 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 >>> >>> 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. > > 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/, 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> > thanks, > bartz > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Mark Wilden
2009-Mar-02 17:12 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On Mon, Mar 2, 2009 at 8:23 AM, Bart Zonneveld <zuperinfinite at 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
Bart Zonneveld
2009-Mar-03 09:15 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
On 2-mrt-2009, at 18:12, Mark Wilden wrote:> On Mon, Mar 2, 2009 at 8:23 AM, Bart Zonneveld > <zuperinfinite at 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?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
MAwiniarski
2009-Mar-04 09:55 UTC
[rspec-users] be_valid (validates_format_of ..., :on => :create)
> 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.
Seemingly Similar Threads
- rspec model testing - test on user defined validation- How do I test that the create failed.
- Failure Messages in RSpec
- [Cucumber] Struggling with "multiple step definitions"
- Specifing methods in a steps_for block
- How to write a test for validates_uniqueness_of