Class: RSpec::Interactive::ConfigCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-interactive/rspec_config_cache.rb

Overview

Defined Under Namespace

Classes: RecordingProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#config_proxyObject

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_examplesObject

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

Returns:

  • (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_configurationObject



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.recorded_messages.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_examplesObject



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_examplesObject



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