views:

47

answers:

2

I'm trying to assert that the last record did not get deleted in rails model unit test. I raise an exception if the record.count.one? is true. Initially there are two records.

Edited: There is a user story that says you can delete the users. You cannot delete the user that you are logged in with. (functional test) You cannot delete the last user. (unit test)

A: 

here it is:

  test "verify cannot destroy last user" do
    assert_raise(RuntimeError) {
      User.find(:all).select {|u| u.destroy} }
    assert_equal 1, User.count
  end
Gutzofter
A: 

Here's my literal translation of what you are asking (I think):

last_user = User.last
...
assert_equal last_user, User.last

Here's more traditional test code that is a bit less fragile:

assert_difference('User.count',-1) do
  ...
end

(But Gutzofter may actually be onto what you're looking for.)

ndp
Gutzofter is the one looking for it ;)
theIV