[rspec-users] be_valid (validates_format_of ..., :on => :create)

Bart Zonneveld zuperinfinite at gmail.com
Tue Mar 3 04:15:37 EST 2009

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

describe Article
   before(:each) do
     @article = Article.new
     @valid_attributes = { :title => "Title" }

   it "should not be valid without a title" do
     @article.attributes = @valid_attributes.except :title
     @article.should_not be_valid

   it "should be valid with all valid attributes" do
     @article.attributes = @valid_attributes
     @article.should be_valid

More information about the rspec-users mailing list