Forum: RSpec Spec::Rails, "model.should have(1).error_on(:attribute)" passes when?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
584a6682a2defd3bc5f7dc85fd6e263d?d=identicon&s=25 Bjorn Boulder (bornboulder)
on 2009-03-24 00:16
(Received via mailing list)
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/class...

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
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-24 02:03
(Received via mailing list)
On Mon, Mar 23, 2009 at 6:10 PM, Bjorn Boulder <bornboulder77@gmail.com>
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
799443e8fa20dc3598d7d2d1e30048d5?d=identicon&s=25 Phor Gruber (phor_gruber)
on 2009-03-25 19:37
I added:
  - validates_presence_of :attribute

To my model.

The test now passes.

yay!!

Thanks David.
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-03-25 21:33
(Received via mailing list)
On Mar 25, 2009, at 1:37 PM, Phor Gruber <lists@ruby-forum.com> 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
This topic is locked and can not be replied to.