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.
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.