roberto belardo
2008-Mar-17 10:53 UTC
[rspec-users] 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 => "foo at bar.com" } 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. http://it.docs.yahoo.com/mail/overview/index.html
David Chelimsky
2008-Mar-17 11:16 UTC
[rspec-users] RSpec''ing model association callbacks
On Mon, Mar 17, 2008 at 5:53 AM, roberto belardo <backslash451 at yahoo.it> wrote:> 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 nilThis 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> end > > end > > ----------------- Factory Module > > def self.create_user(attributes = {}) > default_attributes = { > :first_name => "Foo", > :second_name => "Bar", > :user_name => "FooBar", > :email => "foo at bar.com" > } > 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.
On 3/17/08, David Chelimsky <dchelimsky at gmail.com> wrote:> On Mon, Mar 17, 2008 at 5:53 AM, roberto belardo <backslash451 at yahoo.it> wrote: > > 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 > > > 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, > > DavidOr perhaps Comment.find_all_by_user_id(user.id).should be_empty -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/
roberto belardo
2008-Mar-17 11:30 UTC
[rspec-users] RSpec''ing model association callbacks
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 Chelimsky <dchelimsky at gmail.com> ha scritto:> On Mon, Mar 17, 2008 at 5:53 AM, roberto belardo > <backslash451 at yahoo.it> wrote: > > 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 > > 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 > > > > end > > > > end > > > > ----------------- Factory Module > > > > def self.create_user(attributes = {}) > > default_attributes = { > > :first_name => "Foo", > > :second_name => "Bar", > > :user_name => "FooBar", > > :email => "foo at bar.com" > > } > > 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. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >Inviato da Yahoo! Mail. Il servizio di posta?con lo spazio illimitato. http://it.docs.yahoo.com/mail/overview/index.html
David Chelimsky
2008-Mar-17 11:33 UTC
[rspec-users] RSpec''ing model association callbacks
On Mon, Mar 17, 2008 at 6:28 AM, Rick DeNatale <rick.denatale at gmail.com> wrote:> > On 3/17/08, David Chelimsky <dchelimsky at gmail.com> wrote: > > On Mon, Mar 17, 2008 at 5:53 AM, roberto belardo <backslash451 at yahoo.it> wrote: > > > 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 > > > > > > 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 > > Or perhaps > > Comment.find_all_by_user_id(user.id).should be_empty+1
Seemingly Similar Threads
- Missing methods
- Final Opinion :: Need final opinions on the best way to handle arrays in forms
- Newbie Question: fom_tag_remote return HTML
- Newbie''s problem with a nil object he didn''t expect!
- Once I added this HABTM, one of my 'through' relationships, on a non-habtm model, seems to have broke?