rspec-openapi icon indicating copy to clipboard operation
rspec-openapi copied to clipboard

Preserving a predictable output

Open blocknotes opened this issue 3 years ago • 1 comments
trafficstars

Hey :) Using this gem we noticed that having predictable data in different run can be useful if you are going to regenerate the OpenAPI documentation (fully or partially).

To achieve this goal we are using this configuration block:

if ENV['OPENAPI'].present?
  seed = ENV.fetch('OPENAPI_SEED', 0xFFFF)
  srand(seed)
  Faker::Config.random = Random.new(seed) if defined? Faker

  RSpec.configure do |config|
    config.order = :defined

    config.before do
      allow(Devise).to receive(:friendly_token) { ('A'..'Z').to_a.sample(20).join } if defined? Devise

      ActiveRecord::Base.connection.tables.each do |t|
        # to preserve IDs
        ActiveRecord::Base.connection.reset_pk_sequence!(t)
      end
    end

    config.include ActiveSupport::Testing::TimeHelpers

    config.around do |example|
      time = Time.zone.local(2022, 10, 15, 12, 34, 56)
      travel_to(time) # to preserve dates & times
      example.run
      travel_back
    end
  end
end

Perhaps these kind of options shouldn't be included in the gem (they are configuration) but they could be useful in the README to help others.

WDYT?

blocknotes avatar Oct 13 '22 13:10 blocknotes

@blocknotes Sorry for late response. Adding example like this sounds great to me. PR welcome.

exoego avatar Oct 28 '22 06:10 exoego