exoego / rspec-openapi

Generate OpenAPI schema from RSpec request specs
MIT License
428 stars 63 forks source link

Preserving a predictable output #76

Open blocknotes opened 1 year ago

blocknotes commented 1 year ago

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?

exoego commented 1 year ago

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