Class: RSpec::Interactive::ConfigCache
- Inherits:
-
Object
- Object
- RSpec::Interactive::ConfigCache
- Defined in:
- lib/rspec-interactive/rspec_config_cache.rb
Overview
Defined Under Namespace
Classes: RecordingProxy
Instance Attribute Summary collapse
-
#config_proxy ⇒ Object
We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what's running, what are the different test results, etc).
-
#root_shared_examples ⇒ Object
We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what's running, what are the different test results, etc).
Instance Method Summary collapse
- #ensure_configuration_setter! ⇒ Object
- #forward_rspec_config_singleton_to(config_proxy) ⇒ Object
- #has_recorded_config? ⇒ Boolean
- #record_configuration(&configuration_block) ⇒ Object
- #replay_configuration ⇒ Object
- #restore_shared_examples ⇒ Object
- #stash_shared_examples ⇒ Object
Instance Attribute Details
#config_proxy ⇒ Object
We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what's running, what are the different test results, etc).
RSpec.configuration gets also loaded with a bunch of stuff from the 'spec/spec_helper.rb' file. Often that instance is extended with other modules (FactoryGirl, Mocha,...) and we don't want to replace requires with load all around the place.
Instead, we proxy and record whatever is done to RSpec.configuration during the first invocation of require('spec_helper'). This is done by interposing the RecordingProxy class on of RSpec.configuration.
15 16 17 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 15 def config_proxy @config_proxy end |
#root_shared_examples ⇒ Object
We have to reset the RSpec.configuration, because it contains a lot of information related to the current test (what's running, what are the different test results, etc).
RSpec.configuration gets also loaded with a bunch of stuff from the 'spec/spec_helper.rb' file. Often that instance is extended with other modules (FactoryGirl, Mocha,...) and we don't want to replace requires with load all around the place.
Instead, we proxy and record whatever is done to RSpec.configuration during the first invocation of require('spec_helper'). This is done by interposing the RecordingProxy class on of RSpec.configuration.
15 16 17 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 15 def root_shared_examples @root_shared_examples end |
Instance Method Details
#ensure_configuration_setter! ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 87 def ensure_configuration_setter! return if RSpec.respond_to?(:configuration=) ::RSpec.instance_eval do def self.configuration=(value) @configuration = value end end end |
#forward_rspec_config_singleton_to(config_proxy) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 65 def forward_rspec_config_singleton_to(config_proxy) # an old version of rspec-rails/lib/rspec/rails/view_rendering.rb adds # methods on the configuration singleton. This takes care of that. ::RSpec.configuration.singleton_class .send(:define_method, :method_missing, &config_proxy.method(:send)) end |
#has_recorded_config? ⇒ Boolean
61 62 63 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 61 def has_recorded_config? !!self.config_proxy end |
#record_configuration(&configuration_block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 30 def record_configuration(&configuration_block) ensure_configuration_setter! original_config = ::RSpec.configuration ::RSpec.configuration = RecordingProxy.new(original_config, []) configuration_block.call # spec helper is called during this yield, see #reset self.config_proxy = ::RSpec.configuration ::RSpec.configuration = original_config stash_shared_examples forward_rspec_config_singleton_to(self.config_proxy) end |
#replay_configuration ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 46 def replay_configuration ::RSpec.configure do |config| self.config_proxy..each do |method, args, block| # reporter caches config.output_stream which is not good as it # prevents the runner to use a custom stdout. next if method == :reporter config.send(method, *args, &block) end end restore_shared_examples forward_rspec_config_singleton_to(self.config_proxy) end |
#restore_shared_examples ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 76 def restore_shared_examples shared_example_groups = ::RSpec.world.shared_example_group_registry.send(:shared_example_groups) shared_example_groups.clear self.root_shared_examples.each do |context, hash| hash.each do |name, shared_module| shared_example_groups[context][name] = shared_module end end end |
#stash_shared_examples ⇒ Object
72 73 74 |
# File 'lib/rspec-interactive/rspec_config_cache.rb', line 72 def stash_shared_examples self.root_shared_examples = ::RSpec.world.shared_example_group_registry.send(:shared_example_groups).dup end |