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.
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (Guest)
on 2006-05-06 22: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!
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-05-06 23: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!
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (Guest)
on 2006-05-07 04: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
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-05-07 04: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
Ab4c5cd5d9cc028fcba7a5eec8e1bf30?d=identicon&s=25 Alain Pilon (Guest)
on 2006-05-07 05: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?
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-05-07 05: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?
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-08 23: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 Hodel - drbrain@segment7.net - 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.