Minitest calls method twice?

I’m having an issue with minitest. Consider this (simplified) example test

# The test
test "the truth" do
  assert_equal true, ParentalLeaveCalculator.new(@employee).check_parental_leave_paid(@timesheet, 1, 8)
end

# The method
def check_parental_leave_paid(timesheet, day, current_hours)
  ...
  used_hours_paid = calculate_used_hours_paid(current_hours)
  puts "USED HOURS: #{used_hours_paid}"
  ...
end

# The test result
USED HOURS: 361.0
USED HOURS: 16.0
F

It does 1 assertion by calling the ParentalLeaveCalculator. Why does it show the puts twice when running the test?

Curious indeed!

No chance #check_parental_leave_paid is being called in the ParentalLeaveCalculator model’s initializer or anywhere else? (Or maybe as some by-product of calling #calculate_used_hours_paid?)

If you put this line at the top of #check_parental_leave_paid it will dump out a short trace of the last 5 places leading up to where it has been called from!

puts caller[0..4].inspect

Found it! There is a model callback being triggered which also calls this method. Thanks for the puts caller[0..4].inspect!!! That’s how I found out.

1 Like