Module: DataShifter::SpecHelper

Defined in:
lib/data_shifter/spec_helper.rb

Overview

Test helpers for RSpec. Include this module in your spec_helper or rails_helper:

require "data_shifter/spec_helper"

RSpec.configure do |config|
  config.include DataShifter::SpecHelper, type: :data_shift
end

Or include it in individual specs:

RSpec.describe DataShifts::BackfillFoo do
  include DataShifter::SpecHelper
  ...
end

Instance Method Summary collapse

Instance Method Details

#capture_data_shift_outputObject

Run a shift and capture its output. Returns [Axn::Result, String] tuple.

Examples:

result, output = capture_data_shift_output do
  run_data_shift(DataShifts::BackfillFoo)
end
expect(output).to include("DRY RUN")


65
66
67
68
69
70
71
72
73
# File 'lib/data_shifter/spec_helper.rb', line 65

def capture_data_shift_output
  original_stdout = $stdout
  $stdout = StringIO.new
  result = yield
  output = $stdout.string
  [result, output]
ensure
  $stdout = original_stdout
end

#run_data_shift(shift_class, dry_run: true, commit: false) ⇒ Axn::Result

Run a data shift class with the given options. Returns the Axn::Result.

Examples:

result = run_data_shift(DataShifts::BackfillFoo)
expect(result).to be_ok

with commit

result = run_data_shift(DataShifts::BackfillFoo, commit: true)
expect(record.reload.foo).to eq("bar")

Parameters:

  • shift_class (Class)

    the DataShifter::Shift subclass

  • dry_run (Boolean) (defaults to: true)

    whether to run in dry_run mode (default: true)

  • commit (Boolean) (defaults to: false)

    shorthand for dry_run: false (default: false)

Returns:

  • (Axn::Result)


36
37
38
39
# File 'lib/data_shifter/spec_helper.rb', line 36

def run_data_shift(shift_class, dry_run: true, commit: false)
  effective_dry_run = commit ? false : dry_run
  shift_class.call(dry_run: effective_dry_run)
end

#silence_data_shift_outputObject

Suppress STDOUT output during a block (useful for cleaner test output).

Examples:

silence_data_shift_output do
  run_data_shift(DataShifts::BackfillFoo, commit: true)
end


48
49
50
51
52
53
54
# File 'lib/data_shifter/spec_helper.rb', line 48

def silence_data_shift_output
  original_stdout = $stdout
  $stdout = StringIO.new
  yield
ensure
  $stdout = original_stdout
end