Forum: Ruby on Rails assert_raises --> how does it work?

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.
Alain Pilon (Guest)
on 2006-05-07 00:00
Hi,

I have this code fragment:

  def test_delete_concept
    assert_not_nil Concept.find(concepts(:entreprise).id)
    get :delete_concept, {:concept_id => concepts(:entreprise).id}
    assert_raises :RecordNotFound,
Concept.find(concepts(:entreprise).id)
  end

basicaly, on the first line, I make sure that the concept exist in the
DB. On the second line I delete the concept fron the DB.

Finaly, this is where I have a problem, on the third line, I want to see
if the record was deleted. I though about using the assert_raises method
since a #find call to a non existant record would raise a
:RecordNotFound exception. But it doesnt work. The system simply tells
me that it can find a record with the provided ID and thats all... The
test fails on that line.

Any idea how I can do that test, or how I can use assert_raises?

Thanks!
Mick S. (Guest)
on 2006-05-07 01:36
You need:

assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id) }

since the code to be tested must be in a block - assert_raises will
perform a yield on that block.


Alain Pilon wrote:
> Hi,
>
> I have this code fragment:
>
>   def test_delete_concept
>     assert_not_nil Concept.find(concepts(:entreprise).id)
>     get :delete_concept, {:concept_id => concepts(:entreprise).id}
>     assert_raises :RecordNotFound,
> Concept.find(concepts(:entreprise).id)
>   end
>
> basicaly, on the first line, I make sure that the concept exist in the
> DB. On the second line I delete the concept fron the DB.
>
> Finaly, this is where I have a problem, on the third line, I want to see
> if the record was deleted. I though about using the assert_raises method
> since a #find call to a non existant record would raise a
> :RecordNotFound exception. But it doesnt work. The system simply tells
> me that it can find a record with the provided ID and thats all... The
> test fails on that line.
>
> Any idea how I can do that test, or how I can use assert_raises?
>
> Thanks!
Alain Pilon (Guest)
on 2006-05-07 06:18
Thanks Mick but it doesnt work. I tred that line:

 assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id)
}

and  got that message:

generic_controller_test.rb:46: parse error, unexpected '{', expecting
kEND
    assert_raises :RecordNotFound {
Concept.find(concepts(:entreprise).id) }
                                   ^
generic_controller_test.rb:46: parse error, unexpected '}', expecting
kEND
generic_controller_test.rb:97: parse error, unexpected $, expecting kEND


any idea?

thanks
Mick S. (Guest)
on 2006-05-07 06:54
Hmmn...asert_raises appears to be deprecated, and I can't find any
documentation on it. Try using assert_raise instead, and put the
argument in parentheses. Also, make an explicit class reference to
RecordNotFound, thus:

assert_raise(ActiveRecord::RecordNotFound) {
  Concept.find(concepts(:entreprise).id)
}

If you still get syntax errors, it may be to do with some earlier code.

HTH, Mick

Alain Pilon wrote:
> Thanks Mick but it doesnt work. I tred that line:
>
>  assert_raises :RecordNotFound { Concept.find(concepts(:entreprise).id)
> }
>
> and  got that message:
>
> generic_controller_test.rb:46: parse error, unexpected '{', expecting
> kEND
>     assert_raises :RecordNotFound {
> Concept.find(concepts(:entreprise).id) }
>                                    ^
> generic_controller_test.rb:46: parse error, unexpected '}', expecting
> kEND
> generic_controller_test.rb:97: parse error, unexpected $, expecting kEND
>
>
> any idea?
>
> thanks
Alain Pilon (Guest)
on 2006-05-07 07:18
Thanks, its working now!

Received the same tip from Jeremy, so thanks to you too! Do you know if
the new Agile dev on RoR book from pragmatic talk about all the changes
affecting testing? Or if they devote more pages to it?
Mick S. (Guest)
on 2006-05-07 07:33
I haven't looked at it in any detail yet, but they have added a section
on the new integration testing facilities.

Mick

Alain Pilon wrote:
> Thanks, its working now!
>
> Received the same tip from Jeremy, so thanks to you too! Do you know if
> the new Agile dev on RoR book from pragmatic talk about all the changes
> affecting testing? Or if they devote more pages to it?
Eric H. (Guest)
on 2006-05-09 01:08
(Received via mailing list)
On May 6, 2006, at 7:18 PM, Alain Pilon wrote:

>     assert_raises :RecordNotFound {
> Concept.find(concepts(:entreprise).id) }
>                                    ^
> generic_controller_test.rb:46: parse error, unexpected '}', expecting
> kEND
> generic_controller_test.rb:97: parse error, unexpected $, expecting
> kEND
>
>
> any idea?

{ and do have different precedence, { binds to the closest while do
to the farthest.  Switch { } for do end or add parens to bind it to
the method call.

--
Eric H. - removed_email_address@domain.invalid - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.