minitest-stub-const 0.4 → 0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +43 -8
- data/lib/minitest/stub_const.rb +34 -28
- data/test/test_stub_const.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c368abae48ce6602249a2ab7e84151f0c0791bc
|
4
|
+
data.tar.gz: 604721ca05230092a7d3908d062c3643c6a9f41c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6096aee46275edcd51e6675c35c80ac223ca6d6062dd4fd91304a6e736a189a2d103fb84d7a61de9414a1fe1c51a6957129e92c2a05477ad3599c58248c6eba
|
7
|
+
data.tar.gz: 1b1e4d2376d0bbb965ff4b1b0b7809775a975070f81cec2ed350aa0893ad18c023580a5c150aa4227fac449d572e9376096be0fd0f85f6aabebc6c7a26eedc33
|
data/README.md
CHANGED
@@ -7,20 +7,55 @@ Like RSpec's [stub_const] [rspec], but boring and non-magical.
|
|
7
7
|
|
8
8
|
|
9
9
|
## Example
|
10
|
+
Stub a constant for the duration of a block:
|
10
11
|
|
11
12
|
```ruby
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
MyLib.stub_const(:Thing, m) do
|
17
|
-
@subject.add_thing
|
18
|
-
end
|
13
|
+
module Foo
|
14
|
+
BAR = :original
|
15
|
+
end
|
19
16
|
|
20
|
-
|
17
|
+
Foo.stub_const(:BAR, :stubbed) do
|
18
|
+
Foo::BAR
|
21
19
|
end
|
20
|
+
# => :stubbed
|
21
|
+
|
22
|
+
Foo::BAR
|
23
|
+
# => :original
|
22
24
|
```
|
23
25
|
|
26
|
+
This is especially useful when testing that the expected class methods
|
27
|
+
are being called on a `Module` or `Class` instance:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
module SomeLib
|
31
|
+
class Thing
|
32
|
+
def self.add
|
33
|
+
fail NotImplementedError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class ThingAdder
|
39
|
+
def add_thing
|
40
|
+
SomeLib::Thing.add
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ThingAdder do
|
45
|
+
describe '#add_thing' do
|
46
|
+
it 'should call Thing.add' do
|
47
|
+
adder = ThingAdder.new
|
48
|
+
mock = Minitest::Mock.new
|
49
|
+
mock.expect(:add, nil)
|
50
|
+
|
51
|
+
SomeLib.stub_const(:Thing, mock) do
|
52
|
+
adder.add_thing
|
53
|
+
end
|
54
|
+
assert mock.verify
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
24
59
|
|
25
60
|
## Installation
|
26
61
|
|
data/lib/minitest/stub_const.rb
CHANGED
@@ -1,51 +1,57 @@
|
|
1
1
|
class Object
|
2
|
-
|
3
|
-
#
|
4
|
-
#
|
5
|
-
# is useful when testing that the expected class methods are being called on
|
6
|
-
# a Module or Class instance.
|
2
|
+
# Replace the +value+ of constant +name+ for the duration of a
|
3
|
+
# +block+. This is especially useful when testing that the expected
|
4
|
+
# class methods are being called on a Module or Class instance.
|
7
5
|
#
|
8
6
|
# Example:
|
9
7
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# MyLib.stub_const(:Thing, m) do
|
14
|
-
# @subject.add_thing(:whatever)
|
8
|
+
# module Foo
|
9
|
+
# BAR = :original
|
15
10
|
# end
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
11
|
+
#
|
12
|
+
# Foo.stub_const(:BAR, :stubbed) do
|
13
|
+
# Foo::BAR
|
14
|
+
# end
|
15
|
+
# # => :stubbed
|
16
|
+
#
|
17
|
+
# Foo::BAR
|
18
|
+
# # => :original
|
19
19
|
def stub_const(name, val, &block)
|
20
|
-
|
21
|
-
|
22
|
-
silence_warnings
|
23
|
-
const_set(name, val)
|
24
|
-
end
|
25
|
-
|
20
|
+
defined = const_defined?(name)
|
21
|
+
orig = const_get(name) if defined
|
22
|
+
silence_warnings { const_set(name, val) }
|
26
23
|
yield
|
27
24
|
ensure
|
28
|
-
|
29
|
-
const_set(name, orig)
|
25
|
+
if defined
|
26
|
+
silence_warnings { const_set(name, orig) }
|
27
|
+
else
|
28
|
+
remove_const(name)
|
30
29
|
end
|
31
30
|
end
|
32
31
|
|
33
32
|
# Remove the constant +name+ for the duration of a block. This is
|
34
33
|
# useful when testing code that checks whether a constant is defined
|
35
|
-
# or not.
|
34
|
+
# or not. Simply yields to the passed block if the constant is not
|
35
|
+
# currently defined.
|
36
36
|
#
|
37
37
|
# Example:
|
38
38
|
#
|
39
39
|
# Object.stub_remove_const(:File) do
|
40
|
-
# "Look ma, no File!" unless defined(File)
|
40
|
+
# "Look ma, no File!" unless defined?(File)
|
41
41
|
# end
|
42
42
|
# # => "Look ma, no File!"
|
43
43
|
def stub_remove_const(name)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
if const_defined?(name)
|
45
|
+
begin
|
46
|
+
orig = const_get(name)
|
47
|
+
remove_const(name)
|
48
|
+
yield
|
49
|
+
ensure
|
50
|
+
const_set(name, orig)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
yield
|
54
|
+
end
|
49
55
|
end
|
50
56
|
|
51
57
|
# Add a minimal implementation of ActiveSupport's silence_warnings if it
|
data/test/test_stub_const.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require File.expand_path('../../lib/minitest/stub_const', __FILE__)
|
2
|
+
require 'minitest/autorun'
|
2
3
|
require 'minitest/mock'
|
3
4
|
|
4
5
|
module A
|
@@ -20,6 +21,7 @@ describe 'Object' do
|
|
20
21
|
A.stub_const(:B, @mock) do
|
21
22
|
assert_equal :new, A::B.what
|
22
23
|
end
|
24
|
+
@mock.verify
|
23
25
|
end
|
24
26
|
|
25
27
|
it 'restores the original value after the block' do
|
@@ -30,6 +32,15 @@ describe 'Object' do
|
|
30
32
|
it 'does not raise any warnings' do
|
31
33
|
assert_silent { A.stub_const(:B, @mock) { } }
|
32
34
|
end
|
35
|
+
|
36
|
+
it 'should stub undefined constants' do
|
37
|
+
refute defined?(A::X)
|
38
|
+
A.stub_const(:X, @mock) do
|
39
|
+
assert_equal :new, A::X.what
|
40
|
+
end
|
41
|
+
refute defined?(A::X)
|
42
|
+
@mock.verify
|
43
|
+
end
|
33
44
|
end
|
34
45
|
|
35
46
|
describe '#stub_remove_const' do
|
@@ -47,5 +58,11 @@ describe 'Object' do
|
|
47
58
|
it 'does not raise any warnings' do
|
48
59
|
assert_silent { A.stub_remove_const(:B) { } }
|
49
60
|
end
|
61
|
+
|
62
|
+
it 'leaves undefined constants undefined' do
|
63
|
+
refute defined?(A::X)
|
64
|
+
A.stub_remove_const(:X) { }
|
65
|
+
refute defined?(A::X)
|
66
|
+
end
|
50
67
|
end
|
51
68
|
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.
|
4
|
+
version: '0.5'
|
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-
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|