[rspec-users] Rspec not testing my callback

Andrew Davis lists at ruby-forum.com
Mon Nov 15 10:03:50 EST 2010

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|

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 => [

    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 =

    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

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

Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list