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

David Chelimsky dchelimsky at gmail.com
Thu Feb 12 14:59:40 EST 2009


On Feb 12, 2009, at 1:03 PM, 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

This matcher, have(1).error_on, does call #valid.

>
>  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.

In this case, yes, because the matcher needs that.

> If RSpec does in fact call #valid? automatically, should we refrain  
> from manually calling #valid?

The fact that you are asking this shows that we're violating the  
principle of least surprise. We could make it so it doesn't validate,  
but that would pit the onus on users to validate explicitly (not to  
mention the upgrade burden).

Thoughts?

David
>
> -Nick
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list