[rspec-users] specs for attributes with default values on the SQL layer

svoop
2009-04-16T17:43:50Z

Hi

I''m not sure what would be best practise to treat this case.

Migration:
t.boolean :fetched, :null => false, :default => false

Model:
validates_inclusion_of :fetched, :in => [true, false]

Spec:
it do
  article = Article.new(valid_attributes.except(:fetched))
  article.should have(1).error_on(:fetched)
end

Like this the spec will always fail because although fetched is excepted from
the hash returned by valid_attributes, it still gets set to false due to the
default value. Therefore no error and therefore the spec booms.

A solution would be to use this instead:
article = Article.new(valid_attributes.with(:fetched => nil))

But that would block refactoring if many attributes are tested for errors.
Ideas?