File: sandbox.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (37 lines) | stat: -rw-r--r-- 1,262 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module RSpec
  module Core
    # A sandbox isolates the enclosed code into an environment that looks 'new'
    # meaning globally accessed objects are reset for the duration of the
    # sandbox.
    #
    # @note This module is not normally available. You must require
    #   `rspec/core/sandbox` to load it.
    module Sandbox
      # Execute a provided block with RSpec global objects (configuration,
      # world) reset.  This is used to test RSpec with RSpec.
      #
      # When calling this the configuration is passed into the provided block.
      # Use this to set custom configs for your sandboxed examples.
      #
      # ```
      # Sandbox.sandboxed do |config|
      #   config.before(:context) { RSpec.current_example = nil }
      # end
      # ```
      def self.sandboxed
        orig_config  = RSpec.configuration
        orig_world   = RSpec.world
        orig_example = RSpec.current_example

        RSpec.configuration = RSpec::Core::Configuration.new
        RSpec.world         = RSpec::Core::World.new(RSpec.configuration)

        yield RSpec.configuration
      ensure
        RSpec.configuration   = orig_config
        RSpec.world           = orig_world
        RSpec.current_example = orig_example
      end
    end
  end
end