Skip to content

Instantly share code, notes, and snippets.

@adelahayepm
Created April 14, 2021 08:53
Show Gist options
  • Save adelahayepm/8323d0b4250da05ad33a74ccc6a4f72c to your computer and use it in GitHub Desktop.
Save adelahayepm/8323d0b4250da05ad33a74ccc6a4f72c to your computer and use it in GitHub Desktop.
Factory Bot helper to create a Dry::Struct with the factory DSL
# frozen_string_literal: true
module FactoryBot
module Syntax
module Default
class DSL
def dry_struct_factory(name, options = {}, &block)
factory(name, options) do
instance_eval do
initialize_with { new(attributes) }
end
instance_eval(&block) if block
end
end
end
end
end
end
FactoryBot.define do
dry_struct_factory :user, class: MyApp::User do
name { 'Bob' }
last_name { 'Vance Refrigeration' }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment