在向收入模型添加自定义验证后,我的测试出现了一些问题。
@member.incomes << [Income.new(starting_date: '2015-01-01', amount: 1200, member: @member),
Income.new(starting_date: '2015-04-01', amount: 1400, member: @member)]
我收到以下错误:wrong number of arguments (1 for 0)
我对debugger
的观察:
第一个记录是正确插入的,但第二个记录是错误的。
我做错了什么 - 有什么想法吗?
更新:
concat方法在这里无济于事。
更多代码:
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
@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)>'
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?'
答案 0 :(得分:1)
我相信错误发生在这里:
latest_income = Income.where(member: member_id).select{|i|i}.max(&:starting_date)
max
是Enumerable
中定义的方法。它不需要参数,因此消息&#34;错误的参数数量(1表示0)&#34;。
您可能需要在ActiveRecord中定义的方法maximum
。它需要一个列名:
latest_income = Income.where(member: member_id).maximum(:starting_date)
答案 1 :(得分:0)
<<
是push
的简写,但我认为在这种情况下你想要实现(如@mudasobwa所说)
@member.incomes << Income.new(...) << Income.new(...)
单独推送两个新实例,或尝试
@member.incomes.concat [Income.new(...), Income.new(...)]
如果你想“合并”两个数组