[rspec-users] be_valid (validates_format_of ..., :on => :create)
dchelimsky at gmail.com
Mon Mar 2 10:50:07 EST 2009
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:
>> 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.should_not be_valid
> 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)
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.
> rspec-users mailing list
> rspec-users at rubyforge.org
More information about the rspec-users