[rspec-users] Is #valid? automatically called?

Pat Maddox pat.maddox at gmail.com
Thu Feb 12 14:37:43 EST 2009


On Thu, Feb 12, 2009 at 11:03 AM, Nick Hoffman <nick at deadorange.com> wrote:
> Does RSpec automatically call #valid? on ActiveRecord models?
>
> For instance, when this example is run:
>
> it 'should reject a nil value' do
>  @form = TimeShiftForm.new :file => nil
>
>  puts "@form.errors.count = <<#{@form.errors.count}>>"
>  @form.should have(1).error_on :file
>  puts "@form.errors.count = <<#{@form.errors.count}>>"
> end
>
> This is printed:
> @form.errors.count = <<0>>
> @form.errors.count = <<1>>
>
> However, I never called @form.valid? , which leads me to believe that RSpec
> called it for me.
>
> If RSpec does in fact call #valid? automatically, should we refrain from
> manually calling #valid? ?

RSpec doesn't call #valid? automatically in all cases...but if you
look at the source for error(s)_on in
spec/rails/extensions/active_record/base.rb you'll see that it does
call #valid? automatically.

Pat


More information about the rspec-users mailing list