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
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!)
RUBY_ENGINE == "jruby" ? "@__jruby_is_not_cat_friendly__" : "@😸"
- 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
32
33
34
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 32
def config
@config ||= Configuration.new
end
|
36
37
38
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 36
def configure
yield config
end
|
.define_let_it_be_alias(name, **default_args) ⇒ Object
84
85
86
87
88
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 84
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
40
41
42
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 40
def modifiers
@modifiers ||= {}
end
|
.module_for(group) ⇒ Object
57
58
59
60
61
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 57
def module_for(group)
modules[group] ||= begin
Module.new.tap { |mod| group.prepend(mod) }
end
end
|
.wrap_with_modifiers(mods, &block) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 44
def wrap_with_modifiers(mods, &block)
return block if mods.empty?
validate_modifiers! mods
-> {
record = instance_eval(&block)
mods.inject(record) do |rec, (k, v)|
LetItBe.modifiers.fetch(k).call(rec, v)
end
}
end
|
Instance Method Details
#let_it_be(identifier, **options, &block) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/test_prof/recipes/rspec/let_it_be.rb', line 90
def let_it_be(identifier, **options, &block)
initializer = proc do
instance_variable_set(:"#{TestProf::LetItBe::PREFIX}#{identifier}", instance_exec(&block))
rescue FrozenError => e
e.message << TestProf::LetItBe::FROZEN_ERROR_HINT
raise
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)
before_all(&initializer)
let_accessor = LetItBe.wrap_with_modifiers(options) do
instance_variable_get(:"#{PREFIX}#{identifier}")
end
LetItBe.module_for(self).module_eval do
define_method(identifier) do
if @__inspect_output.include?("before(:context)") || @__inspect_output.include?("before_all")
instance_variable_get(:"#{PREFIX}#{identifier}")
else
super()
end
end
end
let(identifier, &let_accessor)
end
|