Created
July 24, 2021 13:25
-
-
Save ty-porter/a4791a5454ba79bb1e4097e9b1bdf6ab to your computer and use it in GitHub Desktop.
Fixture factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
FactoryBot.define do | |
factory :export do | |
skip_create | |
initialize_with do | |
new(attributes, type) | |
end | |
transient do | |
type { nil } | |
end | |
end | |
%i[ | |
game | |
scoreboard | |
standings | |
].each do |entity_type| | |
factory "export_#{entity_type}".to_sym, parent: :export do | |
type { entity_type } | |
end | |
end | |
end | |
class Export | |
def initialize(args, type) | |
@payload = JSON.parse(File.read(Rails.root.join("spec/fixtures/exports/#{type}.json"))).deep_symbolize_keys | |
undefined_attributes = args.keys - @payload.keys | |
raise ExportFactoryError, "#{undefined_attributes} not defined in #{type} fixture" if undefined_attributes.present? | |
@payload.merge!(args) | |
end | |
attr_reader :payload | |
end | |
class ExportFactoryError < StandardError; end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment