Home > Enterprise >  How to write rspec for a model method?
How to write rspec for a model method?

Time:10-04

Here's my method

def get_processing_fee_for_given_price(ticket_service_fee, price)
    ((processing_fee_percent / 100.0) * (ticket_service_fee.to_f   price.to_f))
  end

I want to write rspec for this method.

def processing_fee_percent
    3.0
  end

This is my processing_fee_percent method

CodePudding user response:

I would do it like this:

describe '#get_processing_fee_for_given_price' do
  subject(:instance) { described_class.new } 

  it 'returns to expected processing_fee' do
    expect(
      instance.get_processing_fee_for_given_price(5, 100)
    ).to eq 3.15
  end
end

There are accuracy problems when using floats, therefore it is not recommended to use floats for currency calculations. A very simple example of unexpected behavior with floats is this one:

0.1   0.2
#=> 0.30000000000000004

I suggest using BigDecimal instead.

  • Related