Can a method's variable be stubbed with Minitest?

21 Views Asked by At

I'm trying to test the following method's return values. Because the result is based on the account's invoice data and new account's may not have any invoices, the method could either return a Float, Float::NaN or Float::Infinity depending on the specific account scenario.

I would like to test each scenario and I was thinking of stubbing some data to force the returned value, but I haven't been succesful so far.

Here's the method:

  def billed_amount_last_month_change
    past_month_amount = main_value # attr_reader

    previous_month_amount = account.invoices # account is attr_reader
      .where(due_date: (Time.current - 60.days)..(Time.current - 30.days))
      .where(status: %w[paid unpaid scheduled dunning refunded charged_back])
      .sum("amount_cents") / 100

    (((past_month_amount - previous_month_amount) / previous_month_amount.to_f) * 100).round(2)
  end

I've tried something like the following but it does not seem to work. If I put a byebug inside #billed_amount_last_month_change and inspect the previous_month_amount variable it still returns a value based on the fixtures data instead of the stubbed value.

test "#billed_amount_last_month_change may return a Float" do
    @account = accounts(:one)
    main_value = 3_750
    @billing_calculator = Metrics::Calculators::Kpis::Billing.new(@account, main_value)
    
    @account.invoices.expects(:sum).returns(7_500)
    expected_result = 100

    result = @billing_calculator.send(:billed_amount_last_month_change)

    assert_equal expected_result, result
end
0

There are 0 best solutions below