简体   繁体   中英

wrong number of arguments (1 for 0) in rspec

I some problems with my tests after adding a custom validation to the income model.

@member.incomes << [Income.new(starting_date: '2015-01-01', amount: 1200, member: @member), 
                    Income.new(starting_date: '2015-04-01', amount: 1400, member: @member)]

I get the following error: wrong number of arguments (1 for 0)

My observation with the debugger :
the first record is correctly inserted, but the 2nd one is getting the error.

What I'm doing wrong - any idea?

UPDATE:
the concat method couldn't help here.

more code:

income.rb
validate :newly_added_income_is_also_the_newest?
....
..
def newly_added_income_is_also_the_newest?
  latest_income = Income.where(member: member_id).select{|i|i}.max(&:starting_date)
  return true if latest_income.nil?
  if !latest_income.nil? && latest_income.starting_date >= starting_date
    errors.add(:income, "newest income should be also the latest income of the member '#{member.full_name}''")
  end
end

test: [Budget] should get all incomes which are in between of the start_date and end_date

@member.incomes.concat[Income.new(starting_date: '2015-01-01', amount: 1200, member: @member),
                            Income.new(starting_date: '2015-04-01', amount: 1400, member: @member)]
     budget = FactoryGirl.create(:budget, donation: @donation, member: @member)
     expect(budget.get_all_incomes_for_budget_duration.size).to eq(3) 

log1

    1) Budget Tests with a budget based donation types [Budget] should get all incomes which are in between of the start_date and end_date
         Failure/Error: @member.incomes.concat[Income.new(starting_date: '2015-01-01', amount: 1200, member: @member),
         TypeError:
           no implicit conversion of Income into Integer
         # ./spec/models/budget_spec.rb:44:in `[]'
         # ./spec/models/budget_spec.rb:44:in `block (3 levels) in <top (required)>'  

test2: [Budget] should get an adapted promise for income change between the budget range (FAILED - 2)

it '[Budget] should get an adapted promise for income change between the budget range', skip_before: true do
   Member.delete_all
   Donation.delete_all
   member = FactoryGirl.create(:member)
   donation = FactoryGirl.create(:majlis_khuddam_donation)
   #income = FactoryGirl.create(:income, member: member)

   member.incomes << Income.new(starting_date: '2015-01-01', amount: 1200, member_id: 12345) <<
       Income.new(starting_date: '2015-03-01', amount: 1000, member_id: 12345)

   budget = FactoryGirl.create(:budget, donation: donation, member: member)

   expect(budget.promise).to eq(123)
 end

log2

  2) Budget Tests with a budget based donation types [Budget] should get an adapted promise for income change between the budget range
       Failure/Error: Income.new(starting_date: '2015-03-01', amount: 1000, member_id: 12345)
       ArgumentError:
         wrong number of arguments (1 for 0)
       # /Users/user/.rvm/gems/ruby-2.2.0@maalify/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:48:in `__temp__374716274796e676f546164756'
       # ./app/models/income.rb:22:in `each'
       # ./app/models/income.rb:22:in `max'
       # ./app/models/income.rb:22:in `newly_added_income_is_also_the_newest?'

I believe the error occurs here:

latest_income = Income.where(member: member_id).select{|i|i}.max(&:starting_date)

max is a method defined in Enumerable . It does not take an argument, thus the message "wrong number of arguments (1 for 0)".

You probably want the method maximum , which is defined in ActiveRecord. It takes a column name:

latest_income = Income.where(member: member_id).maximum(:starting_date)

The << is a shorthand for push , but i think in this case you want to achieve either (as @mudasobwa said)

@member.incomes << Income.new(...) << Income.new(...)

which pushes both new instances individually, or try

@member.incomes.concat [Income.new(...), Income.new(...)]

if you want to 'merge' two arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM