Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3361634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:10:35+00:00 2026-05-18T03:10:35+00:00

I have a Rails app with an Order and a Refund model. Order has_many

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:10:36+00:00Added an answer on May 18, 2026 at 3:10 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Video model in my rails app that has_many comments. I
I have a rails app moving along fairly well, but the fact that I'm
I have a model: class Scr < ActiveRecord::Base def self.find_scrs find(:all, :order => id)
I have a rails 3 app that displays user submissions in order of how
I have a Rails app with a few model classes (e.g. Category , Subcategory
I have an existing Rails internal app, that handles inventory, invoicing, order taking, labeling,
I have a Rails 2.x app with no tests. I can write out the
I have a rails 3 app, and am trying to do testing. I do
I have a rails app that I have serving up XML on an infrequent
I have a Rails app that I have successfully tested with Mongrel and Webkit.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.