Forum: Ruby on Rails Unit Testing - Persistence of Destroyed Objects?

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.
24499e811f19343c7dc406104e9b5785?d=identicon&s=25 Belorion (Guest)
on 2006-01-06 18:06
(Received via mailing list)
I was doing some unit testing today and came across something that
surprised
me.  Either I am confused, or destroyed objects persist in memory when
doing
unit testing.

To explain: I have a model, Author, which 'has_many :web_resources'.
One
type of "web resource" for an Author is the authors website.  So, to
facilitate easy creation/lookup of websites for authors, I did the
following
in author.rb (simplified):

def website=( str )
   wr = self.web_resources.first
   # We delete the first web resource if str is nil/empty
   if str.nil? || str == ""
       wr.destroy
       return
   end
   # Otherwise, create/modify website
   <snip>
end

def website()
    self.web_resources.first
end

Now, I was unit testing this today, and something unexpected (to me)
happend.

@author.website=''
# The following test fails
assert_equal nil, @author.website
# However, if I do this, it does *not* fail
assert_equal nil, Author.find( @author.id ).website

Calling @author.website='' is indeed destroying the object, but in the
unit
test calling @author.website the object persists, but if I do a brand
new
Author.find(), it is not there.  Am I missing something here?

-Matt
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-01-06 18:21
(Received via mailing list)
> Calling @author.website='' is indeed destroying the object, but in the unit
> test calling @author.website the object persists, but if I do a brand new
> Author.find(), it is not there.  Am I missing something here?
>
> -Matt

Calling #destroy removes the row from the database, but the ruby object
stays:

>> a = Article.find :first
=> #<Article:0x238be9c @attributes={...snip...}>
>> a.id
=> 1
>> a.destroy
=> #<Article:0x238be9c @attributes={...snip...}>
>> a.title # title attr is still available
=> "Welcome to Mephisto"
>> a.id   # id attr is still available
=> 1
>> Article.find_by_id 1
=> nil     # not in the db anymore however

--
rick
http://techno-weenie.net
This topic is locked and can not be replied to.