Module: Fixtury::MinitestHooks::ClassMethods

Defined in:
lib/fixtury/minitest_hooks.rb

Instance Method Summary collapse

Instance Method Details

#fixtury(*searches, **opts) ⇒ void

This method returns an undefined value.

Declare fixtury dependencies for this test case. This will automatically load the fixtures before the test case is setup, and rollback any changes after the test case is torn down.

Parameters:

  • searches (Array<String>)

    A list of fixture names to load. These should be resolvable paths relative to Fixtury.schema (root).

  • opts (Hash)

    A list of options to customize the behavior of the fixtures. @option opts [Symbol, String, Boolean] :as (true) The name of the accessor method to define for the fixture. If true (default), the last segment will be used.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fixtury/minitest_hooks.rb', line 77

def fixtury(*searches, **opts)
  pathnames = searches.map do |search|
    dfn = Fixtury.schema.get!(search)
    dfn.pathname
  end

  self.fixtury_dependencies += pathnames

  accessor_option = opts[:as]
  accessor_option = opts[:accessor] if accessor_option.nil? # old version, backwards compatability
  accessor_option = accessor_option.nil? ? true : accessor_option

  if accessor_option

    if accessor_option != true && pathnames.length > 1
      raise ArgumentError, "A named :as option is only available when providing one fixture"
    end

    pathnames.each do |pathname|
      method_name = (accessor_option == true ? pathname.split("/").last : accessor_option).to_sym
      ivar = :"@fixtury_#{method_name}"

      class_eval <<-EV, __FILE__, __LINE__ + 1
        def #{method_name}
          return #{ivar} if defined?(#{ivar})

          #{ivar} = fixtury("#{pathname}")
        end
      EV
    end
  end
end