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, 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
.config ⇒ Object
41
42
43
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 41
def config
@config ||= Configuration.new
end
|
45
46
47
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 45
def configure
yield config
end
|
.define_let_it_be_alias(name, **default_args) ⇒ Object
94
95
96
97
98
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 94
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
|
.modifiers ⇒ Object
49
50
51
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 49
def modifiers
@modifiers ||= {}
end
|
.module_for(group) ⇒ Object
67
68
69
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 67
def module_for(group)
modules[group] ||= Module.new.tap { |mod| group.prepend(mod) }
end
|
.wrap_with_modifiers(mods, on: :let, &block) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 53
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
100
101
102
103
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
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 100
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!(metadata[:let_it_be_modifiers]) if metadata[: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
LetItBe.module_for(self).module_eval do
define_method(identifier) do
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
|