Rspec : Declaring a variable that will be created into the method as record

201 Views Asked by At

I'm quite new to rails, but I seem to be facing an issue I cannot solve. I am trying to test a background job that will create a new record and use it later to instanciate a new service.

As I am testing a background job, it seems that I shoud be making a double of this service BEFORE the test, but one of the parameters is created whithin the job, using another record that is created whithin the job as a reference. Hence, there will always be one difference in terms of ID everytime I try to run it.

Here is the sample code :

RSpec.describe UpdateVideoPerfDatasJob, :type => :job do
  it { expect(described_class).to be < ActiveJob::Base }
  context '#perform' do
    let (:split_test) { create(:split_test_for_result_slot) }
    let(:adwords_account) { split_test.adwords_account }
    let(:user) { split_test.adwords_account.user }
    let(:report_service) { instance_double('VideoPerformanceReportBis') }
    let(:api_service) { instance_double('GoogleAdsApiWrapper') }

    let(:campaigns_datas) { '#2 Api called...' }
    let(:segments_datas) { '#3 Api called...' }
    let(:data) { '#4 Global Array Created' }

    before do
      allow(VideoPerformanceReportBis).to receive(:new).with(split_test: split_test, adwords_account: adwords_account, user: user, result_slot: result_slot).and_return(report_service)
      allow(GoogleAdsApiWrapper).to receive(:new).with(adwords_account: adwords_account, user: user).and_return(api_service)
      allow(report_service).to receive(:refresh).with(no_args).and_return('#1 Refresh method')
      allow(report_service).to receive(:call_api_campaigns).with(no_args).and_return(campaigns_datas)
      allow(api_service).to receive(:get_campaigns).with(no_args).and_return('...and campaign gotten !')
      allow(report_service).to receive(:call_api_segments).with(no_args).and_return(segments_datas)
      allow(api_service).to receive(:get_campaigns_segments).with(no_args).and_return('... and segments gotten !')
      allow(report_service).to receive(:create_global_array).with(campaigns_datas, segments_datas).and_return(data)
      allow(report_service).to receive(:update).with(data).and_return('#5 update data')
class UpdateVideoPerfDatasJob < ApplicationJob
  def perform(split_test:, adwords_account:, user:)
    logger.warn 'Updating video performance data'

    history_log = HistoryLog.create(
      split_test: split_test,
      user: user,
      adwords_account: adwords_account,
      operation: 'Update vidéo performance data',
      messages: '',
      status: 'Initiated'
    )

    result_slot = ResultSlot.create(
      split_test: split_test,
      history_log: history_log,
    )

    result_slot.previous_result_slot_id = split_test.result_slot.id unless split_test.result_slot.nil?

    report_service = VideoPerformanceReportBis.new(
      split_test: split_test,
      adwords_account: adwords_account,
      user: user,
      result_slot: result_slot
    )

    response = report_service.refresh
    history_log.update!(
      messages: "Data updated on #{Date.today}",
      status: 'Video performance data updated'
    )
    NotificationsChannel.broadcast_to user, 'Finished Update Video Performance Data Worker'
  end
end

As you can see, the VideoPerformanceReportService is using a result_slot as parameter, that is created inside the job, using an history_log (also created inside the job) as a reference.

How can I test this ?

(Thank you in advance for everyone helping)

0

There are 0 best solutions below