Class: RSpecConsole::ConfigCache

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-console/config_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigCache

Returns a new instance of ConfigCache.



17
18
19
20
21
22
23
# File 'lib/rspec-console/config_cache.rb', line 17

def initialize
  ::RSpec.instance_eval do
    def self.configuration=(value)
      @configuration = value
    end
  end
end

Instance Attribute Details

#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 cache whatever is done to RSpec.configuration during the first invokration of require(‘spec_helper’). This is done by interposing the Proxy class on top of RSpec.configuration.



15
16
17
# File 'lib/rspec-console/config_cache.rb', line 15

def proxy
  @proxy
end

#recorded_configObject

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 cache whatever is done to RSpec.configuration during the first invokration of require(‘spec_helper’). This is done by interposing the Proxy class on top of RSpec.configuration.



15
16
17
# File 'lib/rspec-console/config_cache.rb', line 15

def recorded_config
  @recorded_config
end

#shared_examples_groupsObject

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 cache whatever is done to RSpec.configuration during the first invokration of require(‘spec_helper’). This is done by interposing the Proxy class on top of RSpec.configuration.



15
16
17
# File 'lib/rspec-console/config_cache.rb', line 15

def shared_examples_groups
  @shared_examples_groups
end

Instance Method Details

#cacheObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec-console/config_cache.rb', line 25

def cache
  if self.proxy
    # replay
    ::RSpec.configure do |config|
      self.recorded_config.each do |msg|
        config.send(msg[:method], *msg[:args], &msg[:block])
      end
    end
    ::RSpec.world.shared_example_groups.merge!(self.shared_examples_groups || {}) rescue nil

  else
    # record
    real_config = ::RSpec.configuration
    self.recorded_config = []
    self.proxy = Proxy.new(self.recorded_config, real_config)
    ::RSpec.configuration = self.proxy
    yield
    ::RSpec.configuration = real_config
    self.shared_examples_groups = ::RSpec.world.shared_example_groups.dup rescue nil

    # rspec-rails/lib/rspec/rails/view_rendering.rb add methods on the
    # configuration singleton. Need advice to copy them without going down
    # the road with object2module.
  end

  # Well, instead of copying them, we redirect them to the configuration
  # proxy. Looks like it good enough.
  proxy = self.proxy
  ::RSpec.configuration.singleton_class.send(:define_method, :method_missing) do |method, *args, &block|
    proxy.send(method, *args, &block)
  end

end