require 'rails_helper' module Mks module Rate RSpec.describe RateChangeRequestService do before(:each) do @service = RateChangeRequestService.new end describe '#update_rate' do it 'creates new rates if they do not exist' do chs1 = create(:chargeable_service) chs2 = create(:chargeable_service) create(:chargeable_service) rates = [ { chargeable_service_id: chs1.id, base_rate: 100, utilization_rate: 50 }, { chargeable_service_id: chs2.id, base_rate: 200, utilization_rate: 70 } ] @service.update_rates(rates) expect(ServiceRate.count).to eq 2 end it 'updates existing rates' do sr = create(:service_rate, base_rate: 100, utilization_rate: 20, equipment_item: nil) rates = [ { chargeable_service_id: sr.chargeable_service_id, base_rate: 300 } ] @service.update_rates(rates) sr.reload expect(sr.base_rate).to eq 300 expect(sr.utilization_rate).to eq 20 end end describe '#approve' do it 'changes status to APPROVED' do u = create(:user) r = create(:rate_change_request) allow_any_instance_of(RateChangeRequestService).to receive(:update_rates).and_return(true) result = @service.approve(r, u) r.reload expect(result).to be_truthy expect(r.status).to eq 'APPROVED' expect(r.approved_by).to eq u end end end end end