[rspec-users] Rspec not testing my callback

David Chelimsky dchelimsky at gmail.com
Mon Nov 15 11:14:02 EST 2010


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



More information about the rspec-users mailing list