2010-11-15 98 views
1

我有一个带有订单和退款模型的Rails应用程序。订购has_many:退款。一切都很好。我正在尝试为控制器中的退款逻辑编写功能测试。这就是我现在所拥有的:使用摩卡测试关联方法

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 

和Controller,退款方法是这样的:

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 

一些注意事项:

  1. 我立足的o.refunds.build位在Ryan Bates' Railscast.如果这不正确或不再相关,那是有用的信息。

  2. 我见过很多关于如何实际执行sum方法的冲突信息,其中一些是&,另一些则没有。在script/console&爆炸,但没有它,我得到一个实际的总和。在我的控制,但是,如果我从&:amount:amount切换,我得到这个消息:NoMethodError: undefined method +”为:金额:Symbol`

我觉得有一些概念上的信息丢失,而不是一个错误的地方,所以我会欣赏一些指针。

回答

1

终于想出了这个问题。我将作为一个空关联来存储,而不是将它留在nil中,以便Rails能够处理其他一些方法。所以,当我改变一个,另一个会失败。智慧字:Enumerable#sumActiveRecord::Associations::AssociationCollection#sum采用完全不同的参数。 :)

因此,通过更改存根:refunds => []using a string for the field namesum我得到的东西恢复正常。所以,这里是以上代码的功能版本:

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