# Spec::Rails, "model.should have(1).error\_on(:attribute)" passes when?

**URL:** <https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692>\
**Category:** RSpec\
**Created:** [March 24, 2009, 12:16am UTC](https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692 "2009-03-24T00:16:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bornboulder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bornboulder](https://www.ruby-forum.com/u/bornboulder)\
**Post date:** [March 24, 2009, 12:16am UTC](https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692/1 "2009-03-24T00:16:14Z")

</div>

Spec::Rails people,

I’m curious about:

- model.should have(1).error\_on(:attribute)

displayed in the URL below:  
[http://rspec.rubyforge.org/rspec-rails/1.2.2/classes/Spec/Rails/Extensions/ActiveRecord/InstanceMethods.html](http://rspec.rubyforge.org/rspec-rails/1.2.2/classes/Spec/Rails/Extensions/ActiveRecord/InstanceMethods.html)

The Rspec peepcode screencast suggests that  
model.should have(1).error\_on(:attribute)  
should pass if  
model.send(:attribute)  
returns nil

For me, I get an exception from the rspec script.

My work-around is simple.

Instead of:

- model.should have(1).error\_on(:attribute)  
I Use:  
-model.send(:attribute).should be\_nil

But now I have the question:

- What kind of error causes:
  - model.should have(1).error\_on(:attribute)
  - to pass ??

–b

---

<div class="post-metadata">

**Author:** ![bornboulder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bornboulder](https://www.ruby-forum.com/u/bornboulder)\
**Post date:** [March 24, 2009, 2:03am UTC](https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692/2 "2009-03-24T02:03:11Z")

</div>

On Mon, Mar 23, 2009 at 6:10 PM, Bjorn B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> should pass if  
> -model.send(:attribute).should be\_nil
> 
> But now I have the question:
> 
> - What kind of error causes:
> - model.should have(1).error\_on(:attribute)
> - to pass ??

If you add this to your model:

validates\_presence\_of :attribute

Then this will pass

Model.new(:attribute =\> nil).should have(1).error\_on(:attribute)

Cheers,  
David

---

<div class="post-metadata">

**Author:** ![bornboulder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bornboulder](https://www.ruby-forum.com/u/bornboulder)\
**Post date:** [March 25, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692/3 "2009-03-25T19:37:00Z")

</div>

I added:

- validates\_presence\_of :attribute

To my model.

The test now passes.

yay!!

Thanks David.

---

<div class="post-metadata">

**Author:** ![bornboulder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bornboulder](https://www.ruby-forum.com/u/bornboulder)\
**Post date:** [March 25, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/spec-rails-model-should-have-1-error-on-attribute-passes-when/163692/4 "2009-03-25T21:33:29Z")

</div>

On Mar 25, 2009, at 1:37 PM, Phor G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I added:
> 
> - validates\_presence\_of :attribute
> 
> To my model.
> 
> The test now passes.

Congrats! It took longer than it usually does, but you just  
experienced the red and green parts of the red/green/refactor cycle.

Cheers,  
David
