开发者

Testing association methods with Mocha

I have a Rails app with an Order and a Refund model. Order has_many :refunds. All well and good. I'm trying to write a functional test for refund logic in a controller. Here's what I have right now:

test "should not process partial paypal refund for partially refunded order when refund total plus refund amount is greater than order total" do
    set_super_admin_login_credentials
    o = Order.new
    o.stubs({:id => 1234567, :source => "PayPal", :total => 39.95, :user => users(:dave)})
    Order.stubs(:find).with(1234567).returns(o)
    get :refund, {:order_id => 1234567}
    assert_equal o, assigns(:order)
    o.refunds.build.stubs({:amount => 1.0})
    o.refunds.build.stubs({:amount => 30.00})
    assert_raise do
   开发者_开发问答     post :refund, {:order_id => 1234567, :refund_amount => 10.00}
    end
end

And in the controller, the refund method looks like this:

def refund    
    @order = Order.find(params[:order_id])
    return if request.get?

    amount = params[:refund_amount].to_f
    raise "Cannot refund order for more than total" if (@order.refunds.sum(&:amount) + amount)
    # Do refund stuff
end

Some notes:

  1. I'm basing the o.refunds.build bit on Ryan Bates' Railscast. If this is not right or no longer relevant, that's helpful information.

  2. I've seen a lot of conflicting information about how to actually do the sum method, some with the & and some without. In script/console, the & blows up but without it, I get an actual sum. In my controller, however, if I switch from &:amount to :amount, I get this message: NoMethodError: undefined method+' for :amount:Symbol`

I feel like there's some conceptual information missing rather than a bug somewhere, so I'll appreciate some pointers.


Finally figured out the issue. I was stubbing an empty association as [] rather than leaving it nil for Rails to handle on some other methods. So, when I would change one, the other would fail. Word to the wise: Enumerable#sum and ActiveRecord::Associations::AssociationCollection#sum take entirely different parameters. :)

So, by changing the stubs to leave off :refunds => [] and using a string for the field name in sum I got things back to normal. So, here's the functional version of the above code:

test "should not process partial paypal refund for partially refunded order when refund total plus refund amount is greater than order total" do
    set_super_admin_login_credentials
    o = Order.new
    o.stubs({:id => 1234567, :source => "PayPal", :total => 39.95, :user => users(:dave)})
    Order.stubs(:find).with(1234567).returns(o)
    get :refund, {:order_id => 1234567}
    assert_equal o, assigns(:order)
    o.refunds.build.stubs({:amount => 1.0})
    o.refunds.build.stubs({:amount => 30.00})
    assert_raise do
        post :refund, {:order_id => 1234567, :refund_amount => 10.00}
    end
end

def refund    
    @order = Order.find(params[:order_id])
    return if request.get?

    amount = params[:refund_amount].to_f
    raise "Cannot refund order for more than total" if (@order.refunds.sum('amount') + amount)
    # Do refund stuff
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜