Hello everyone, I have a "before_save" at the top of my model, and it is being used correctly. However, when testing with RSpec/Rcov, my callback method is not being tested. Is there something specific I need to be doing? Thank you, Andrew Davis NASA - KSC -- Posted via http://www.ruby-forum.com/.
On Mon, Nov 15, 2010 at 8:05 AM, Andrew Davis <lists at ruby-forum.com> wrote:> Hello everyone, > > I have a "before_save" at the top of my model, and it is being used > correctly. However, when testing with RSpec/Rcov, my callback method is > not being tested. Is there something specific I need to be doing?Can''t tell what you''re doing at all if you don''t post some code. Please do so. Need to see the spec that''s not doing what you think it should be doing, the implementation of the action invoked by that spec, and the before filter. Cheers, David
Okay, I didn''t think you needed to see any code since it seems general. And I meant "after_save" not "before_save". I guess it''s kind of complicated since I have a lot of associations, but hopefully this will help you. My model class TaskOrder < ActiveRecord::Base after_update :save_product_groups def save_product_groups product_groups.each do |p| p.save(false) end end end One of my tests it "should update the task order''s product group attributes" do @pg1 = Factory(:product_group, :weight => 30, :branch_id => 1) @pg2 = Factory(:product_group, :weight => 20, :branch_id => 2) @pg3 = Factory(:product_group, :weight => 50, :branch_id => 3) update_task_order_instance TaskOrder.new(@attr.merge(:product_groups => [ @pg1, @pg2, @pg3 ])) p1 = update_task_order_instance.product_groups.first p2 = update_task_order_instance.product_groups.second p3 = update_task_order_instance.product_groups.last @pg_hash = { "#{p1.id}" => {"weight" => 40}, "#{p2.id}" => {"weight" => 50}, "#{p3.id}" => {"weight" => 10} } update_task_order_instance.existing_product_group_attributes @pg_hash update_task_order_instance.save update_task_order_instance.product_groups.first.weight.should == 40 update_task_order_instance.product_groups.second.weight.should == 50 update_task_order_instance.product_groups.last.weight.should == 10 update_task_order_instance.product_groups.count.should == 3 end As you can see, there is a line in the test that calls a save method. I''ve set breakpoints on the development server and observed that it DOES use the "save_product_groups" as it should be, but for some reason it''s just not being covered in my rcov. Let me know if you need anything else. Please note that I''m still somewhat new to Rails. Thank you, Andrew Davis NASA - KSC -- Posted via http://www.ruby-forum.com/.
On Nov 15, 2010, at 9:03 AM, Andrew Davis wrote:> Okay, I didn''t think you needed to see any code since it seems general.Even if it''s hypothetical, it''s always better to post code so we have context for a conversation. The general answer to "RSpec is not testing my filters" is "you''re doing it wrong." Posting code gives us something to work with.> And I meant "after_save" not "before_save". > > I guess it''s kind of complicated since I have a lot of associations, but > hopefully this will help you. > > My model > class TaskOrder < ActiveRecord::Base > after_update :save_product_groups > > def save_product_groups > product_groups.each do |p| > p.save(false) > end > end > end > > > One of my tests > it "should update the task order''s product group attributes" do > @pg1 = Factory(:product_group, :weight => 30, :branch_id => 1) > @pg2 = Factory(:product_group, :weight => 20, :branch_id => 2) > @pg3 = Factory(:product_group, :weight => 50, :branch_id => 3) > update_task_order_instance > TaskOrder.new(@attr.merge(:product_groups => [ > @pg1, > @pg2, > @pg3 > ]))This ^^ instantiates a new object but does not save it to the database, so the "save" below creates the new record, therefore the "after_update" filter is not called. You might want that to be "after_save" instead, but you''d have to think about the other implications of that in the context of your app. If you don''t, then change this line, above, to TaskOrder.create and the save, below, will update the record in the db and fire the after_update callback. HTH, David> > p1 = update_task_order_instance.product_groups.first > p2 = update_task_order_instance.product_groups.second > p3 = update_task_order_instance.product_groups.last > > @pg_hash = { > "#{p1.id}" => {"weight" => 40}, > "#{p2.id}" => {"weight" => 50}, > "#{p3.id}" => {"weight" => 10} > } > > update_task_order_instance.existing_product_group_attributes > @pg_hash > > update_task_order_instance.save > update_task_order_instance.product_groups.first.weight.should == 40 > update_task_order_instance.product_groups.second.weight.should == 50 > update_task_order_instance.product_groups.last.weight.should == 10 > update_task_order_instance.product_groups.count.should == 3 > end > > > As you can see, there is a line in the test that calls a save method. > I''ve set breakpoints on the development server and observed that it DOES > use the "save_product_groups" as it should be, but for some reason it''s > just not being covered in my rcov. > > Let me know if you need anything else. Please note that I''m still > somewhat new to Rails. > > Thank you, > > Andrew Davis > NASA - KSC > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Thank you David for your prompt response. I completely understand what you''re getting at regarding the posting of code as well. I think you may have solved my problem, I''ll give it a go later today. I appreciate it! Thanks, Andrew Davis NASA - KSC -- Posted via http://www.ruby-forum.com/.