Topic: In unit testing assert.destroy not working

def test_users

user=User.new(:id=>'4',:ename=>'sai')
     assert user.save
     assert user.destroy
     assert not_nil user   
end

In this code user can be saving but after  destroying the  object (using assert user.destroy)
it contains record (means"assert not_nil user"  failing)
plz give me reply   



thanks in advance

Re: In unit testing assert.destroy not working

Typo? assert_not_nil

Re: In unit testing assert.destroy not working

steph(an) wrote:

Typo? assert_not_nil

i used    assert_nil user
but user contains records.

Re: In unit testing assert.destroy not working

Destroy will remove the record from the database. The in-memory object will still exist (though it is frozen so it can't be modified).

http://api.rubyonrails.com/classes/Acti … ml#M001041

You should be able to check this with the following:

assert_equal true, user.save
uid = user.id
assert_equal true, user.destroy
assert_not_nil user
assert_equal false, User.exists?(uid)

Last edited by steph(an) (2007-09-28 08:22:49)

Re: In unit testing assert.destroy not working

I've done this
   

    assert_raise( ActiveRecord::RecordNotFound ) {
      User.find(4)
    }

Pretty much the same as above (note you don't have to store the uid since you're hardcoding it in there anyway)

Re: In unit testing assert.destroy not working

i tried this code

assert_equal true, user.save
uid = user.id
assert_equal true, user.destroy
assert_not_nil user
assert_equal false, User.exists?(uid)


but assertion failed at     
assert_equal true, user.destroy
even i tried with
assert_equal false, user.destroy
here also assertion failed .plz tell me what happening i unable to get

Re: In unit testing assert.destroy not working

My bad, get rid of the assert_equal true, user.destroy - the API doesn't state that a boolean is returned. In fact, it doesn't state that anything is returned so you shouldn't expect a return value.

Last edited by steph(an) (2007-10-01 04:26:33)