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

Pat Maddox pat.maddox at gmail.com
Thu Apr 16 14:15:26 EDT 2009

On Thu, Apr 16, 2009 at 10:43 AM, svoop <svoop at delirium.ch> wrote:
> 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.

The two examples you gave are very different semantically.  The first
follows "no value was provided, so use the default."  The second is
"an invalid value was provided."

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

What do you mean by it blocks refactorings?  This isn't any different
from the first example, with the exception that you provide a value
instead of letting the default kick in...


More information about the rspec-users mailing list