English 中文(简体)
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
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签