minitest-stub-const 0.5 → 0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c368abae48ce6602249a2ab7e84151f0c0791bc
4
- data.tar.gz: 604721ca05230092a7d3908d062c3643c6a9f41c
3
+ metadata.gz: 509b3a1937e6fe91ce920a6025654d83f8dead26
4
+ data.tar.gz: 12fe651bf614e29b271edc788e878b0e59069cf8
5
5
  SHA512:
6
- metadata.gz: f6096aee46275edcd51e6675c35c80ac223ca6d6062dd4fd91304a6e736a189a2d103fb84d7a61de9414a1fe1c51a6957129e92c2a05477ad3599c58248c6eba
7
- data.tar.gz: 1b1e4d2376d0bbb965ff4b1b0b7809775a975070f81cec2ed350aa0893ad18c023580a5c150aa4227fac449d572e9376096be0fd0f85f6aabebc6c7a26eedc33
6
+ metadata.gz: 4b0bb35ed1ce85a8e89eaf76d3116b7e71d79108d2d55830dc1908fff0f770b1e9e9c3c36600997612653400f17a14b9c41a897122b42eb556bdfdbd1d6fab3d
7
+ data.tar.gz: 947a70ac83373271262508d208c201622963850766d7fd379472cf751ad00e0584a9d5c5178a0bc862fc8beec100692190cc3dfad319628e7f7964979fd30856
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/adammck/minitest-stub-const.svg)](https://travis-ci.org/adammck/minitest-stub-const)
4
4
 
5
5
  Stub constants for the duration of a block in MiniTest.
6
- Like RSpec's [stub_const] [rspec], but boring and non-magical.
6
+ Similar to RSpec's [stub_const] [rspec].
7
7
 
8
8
 
9
9
  ## Example
@@ -51,6 +51,7 @@ describe ThingAdder do
51
51
  SomeLib.stub_const(:Thing, mock) do
52
52
  adder.add_thing
53
53
  end
54
+
54
55
  assert mock.verify
55
56
  end
56
57
  end
@@ -8,24 +8,54 @@ class Object
8
8
  # module Foo
9
9
  # BAR = :original
10
10
  # end
11
- #
11
+ #
12
12
  # Foo.stub_const(:BAR, :stubbed) do
13
13
  # Foo::BAR
14
14
  # end
15
15
  # # => :stubbed
16
- #
16
+ #
17
17
  # Foo::BAR
18
18
  # # => :original
19
- def stub_const(name, val, &block)
20
- defined = const_defined?(name)
21
- orig = const_get(name) if defined
22
- silence_warnings { const_set(name, val) }
19
+ def stub_const(name, val = nil, &block)
20
+ stub_consts(name => val, &block)
21
+ end
22
+
23
+ # Same as stub_const except it supports a Hash of +name+ to +value+ pairs
24
+ # of the constants that will be stubbed for the duration of the +block+.
25
+ #
26
+ # Example:
27
+ #
28
+ # module Foo
29
+ # BAR = :original1
30
+ # BAZ = :original2
31
+ # end
32
+ #
33
+ # Foo.stub_consts(BAR: :stubble, BAZ: :stubby) do
34
+ # [Foo::BAR, Foo::BAZ]
35
+ # end
36
+ # # => [:stubble, :stubby]
37
+ #
38
+ # [Foo::BAR, Foo::BAZ]
39
+ # # => [:original1, :original2]
40
+ def stub_consts(consts, &block)
41
+ original_values = {}
42
+ consts.each_pair do |name, val|
43
+ if const_defined?(name)
44
+ original_value = const_get(name)
45
+ original_values[name] = original_value
46
+ end
47
+ silence_warnings { const_set(name, val) }
48
+ end
49
+
23
50
  yield
51
+
24
52
  ensure
25
- if defined
26
- silence_warnings { const_set(name, orig) }
27
- else
28
- remove_const(name)
53
+ consts.keys.each do |name|
54
+ if original_values.key?(name)
55
+ silence_warnings { const_set(name, original_values[name]) }
56
+ else
57
+ remove_const(name)
58
+ end
29
59
  end
30
60
  end
31
61
 
@@ -8,6 +8,12 @@ module A
8
8
  :old
9
9
  end
10
10
  end
11
+
12
+ module C
13
+ def self.who
14
+ :you
15
+ end
16
+ end
11
17
  end
12
18
 
13
19
  describe 'Object' do
@@ -65,4 +71,46 @@ describe 'Object' do
65
71
  refute defined?(A::X)
66
72
  end
67
73
  end
74
+
75
+ describe '#stub_consts' do
76
+ before do
77
+ @mock = MiniTest::Mock.new
78
+ @mock.expect(:what, :new)
79
+
80
+ @mock2 = MiniTest::Mock.new
81
+ @mock2.expect(:who, :me)
82
+
83
+ @mock3 = MiniTest::Mock.new
84
+ @mock3.expect(:where, :there)
85
+ end
86
+
87
+ def run_stub_block
88
+ A.stub_consts(B: @mock, C: @mock2, D: @mock3) do
89
+ assert_equal :new, A::B.what
90
+ assert_equal :me, A::C.who
91
+ assert_equal :there, A::D.where
92
+ end
93
+ end
94
+
95
+ it 'replaces a set of constants for the duration of a block' do
96
+ run_stub_block
97
+
98
+ @mock.verify
99
+ @mock2.verify
100
+ @mock3.verify
101
+ end
102
+
103
+ it 'restores previous values' do
104
+ run_stub_block
105
+
106
+ assert_equal :old, A::B.what
107
+ assert_equal :you, A::C.who
108
+ end
109
+
110
+ it 'removes constants that were undefined prior to the block' do
111
+ run_stub_block
112
+
113
+ refute A.const_defined?(:D)
114
+ end
115
+ end
68
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-stub-const
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.5'
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Mckaig
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-17 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 2.0.3
45
+ rubygems_version: 2.0.14.1
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: Stub constants for the duration of a block in MiniTest