Created
April 14, 2021 08:53
-
-
Save adelahayepm/8323d0b4250da05ad33a74ccc6a4f72c to your computer and use it in GitHub Desktop.
Factory Bot helper to create a Dry::Struct with the factory DSL
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 | |
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 |
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
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