RSpec'ing model association callbacks

Hi all,
i’m learning rspec and i must admit i really love it.
But at the time i started learning it, i already
developed my models classes and their callbacks.

Now i’m trying to get a 100% coverage of my code but i
cannot reach it because i do not understand how to
spec my callbacks.

Look at this for example:

----------------- User Model

class User < ActiveRecord::Base
before_destroy :delete_associated_comments
def delete_associated_comments
comments = Comment.find_all_by_user_id(self.id)
comments.each { |c|
c.destroy
}
end
end

----------------- User Spec

describe User, " being deleted" do

before(:each) do
end

it “should see deleted his own comments” do
user = Factory.create_user()
comment_1 = Factory.create_comment(:author =>
user)
comment_2 = Factory.create_comment(:author =>
user)
user.destroy
comment_1.should be nil
comment_2.should be nil
end

end

----------------- Factory Module

def self.create_user(attributes = {})
default_attributes = {
:first_name => “Foo”,
:second_name => “Bar”,
:user_name => “FooBar”,
:email => “[email protected]
}
User.create! default_attributes.merge(attributes)
end

def self.create_comment(attributes = {})
default_attributes = {
:title => “title”,
:text => “text”,
:author => create_user
}
Comment.create!
default_attributes.merge(attributes)
end


I imagined a spec like the one above but obviously it
doesn’t works. :frowning:
I really do no understand how i can spec my callbacks.
Could someone help me with this?

Thanks in advance,
Backslas451.

  Inviato da Yahoo! Mail.

Il servizio di posta con lo spazio illimitato.

On Mon, Mar 17, 2008 at 5:53 AM, roberto belardo [email protected]
wrote:

end
comment_1 = Factory.create_comment(:author =>
user)
comment_2 = Factory.create_comment(:author =>
user)
user.destroy
comment_1.should be nil
comment_2.should be nil

This won’t work because you already have a handle on each of these
objects. They are likely deleted from the database, but destroying an
object (in AR terms) does not turn an instance of it to nil.

Give this a shot:

lambda { Comment.find(comment_1.id) }.should
raise_error(ActiveRecord::RecordNotFound)
lambda { Comment.find(comment_2.id) }.should
raise_error(ActiveRecord::RecordNotFound)

Although generally I prefer to keep only one expectation per example.

HTH,
David

On 3/17/08, David C. [email protected] wrote:

Look at this for example:
end
user = Factory.create_user()
objects. They are likely deleted from the database, but destroying an

HTH,

David

Or perhaps

Comment.find_all_by_user_id(user.id).should be_empty


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Mon, Mar 17, 2008 at 6:28 AM, Rick DeNatale [email protected]
wrote:

spec my callbacks.
c.destroy

Although generally I prefer to keep only one expectation per example.

HTH,

David

Or perhaps

Comment.find_all_by_user_id(user.id).should be_empty

+1

Wow, as expected this works. Thank you very much for
your help.
Do you think this approach for model callbacks specing
is right (apart from the number of expectations per
example)?

— David C. [email protected] ha scritto:

cannot reach it because i do not understand how
comments =

comment_1.should be nil
  :second_name => "Bar",
  :text => "text",

doesn’t works. :frowning:

Inviato da Yahoo! Mail.
Il servizio di posta con lo spazio illimitato.