Module: TestProf::LetItBe

Defined in:
lib/test_prof/recipes/rspec/let_it_be.rb

Overview

Just like ‘let`, but persist the result for the whole group. NOTE: Experimental and magical, for more control use `before_all`.

Defined Under Namespace

Modules: Freezer Classes: Configuration, DuplicationError, Modifier

Constant Summary collapse

PREFIX =

Use uniq prefix for instance variables to avoid collisions We want to use the power of Ruby’s unicode support) And we love cats!) Allow overriding the prefix (there are some intermittent issues on JRuby still)

ENV.fetch("LET_IT_BE_IVAR_PREFIX", "@😸")
FROZEN_ERROR_HINT =
"\nIf you are using `let_it_be`, you may want to pass `reload: true` or `refind: true` modifier to it."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject



45
46
47
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 45

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



49
50
51
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 49

def configure
  yield config
end

.define_let_it_be_alias(name, **default_args) ⇒ Object



98
99
100
101
102
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 98

def self.define_let_it_be_alias(name, **default_args)
  define_method(name) do |identifier, **options, &blk|
    let_it_be(identifier, **default_args.merge(options), &blk)
  end
end

.modifiersObject



53
54
55
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 53

def modifiers
  @modifiers ||= {}
end

.module_for(group) ⇒ Object



71
72
73
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 71

def module_for(group)
  modules[group] ||= Module.new.tap { |mod| group.prepend(mod) }
end

.wrap_with_modifiers(mods, on: :let, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 57

def wrap_with_modifiers(mods, on: :let, &block)
  mods = mods.select { |k, val| LetItBe.modifiers.fetch(k).scope == on }
  return block if mods.empty?

  validate_modifiers! mods

  proc do
    record = instance_eval(&block)
    mods.inject(record) do |rec, (k, v)|
      LetItBe.modifiers.fetch(k).call(rec, v)
    end
  end
end

Instance Method Details

#let_it_be(identifier, **options, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 104

def let_it_be(identifier, **options, &block)
  initializer = proc do
    instance_variable_set(:"#{TestProf::LetItBe::PREFIX}#{identifier}", instance_exec(&block))
  rescue FrozenError => e
    raise e.exception("#{e.message}#{TestProf::LetItBe::FROZEN_ERROR_HINT}")
  end

  default_options = LetItBe.config.default_modifiers.dup
  default_options.merge!([:let_it_be_modifiers]) if [:let_it_be_modifiers]

  options = default_options.merge(options)

  initializer = LetItBe.wrap_with_modifiers(options, on: :initialize, &initializer)

  before_all(&initializer)

  let_accessor = LetItBe.wrap_with_modifiers(options, on: :let) do
    instance_variable_get(:"#{PREFIX}#{identifier}")
  end

  report_duplicates(identifier) if LetItBe.config.report_duplicates

  LetItBe.module_for(self).module_eval do
    define_method(identifier) do
      # Trying to detect the context
      # First, check for ::RSpec.current_scope (modern RSpec) and then read @__inspect_output
      # (based on https://github.com/rspec/rspec-rails/commit/7cb796db064f58da7790a92e73ab906ef50b1f34)
      if ::RSpec.respond_to?(:current_scope) && %i[before_all before_context_hook after_context_hook].include?(::RSpec.current_scope)
        instance_variable_get(:"#{PREFIX}#{identifier}")
      elsif /(before|after)\(:context\)/.match?(@__inspect_output) || @__inspect_output.include?("before_all")
        instance_variable_get(:"#{PREFIX}#{identifier}")
      else
        super()
      end
    end
  end

  let(identifier, &let_accessor)
end