minitest-stub-const 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/minitest/stub_const.rb +18 -0
- data/test/test_stub_const.rb +34 -22
- 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: 00edc8c44dd6ae8caa0d4b850178007b01eaf551
|
4
|
+
data.tar.gz: d77cedce7ae177bd65776fb0fef492f122239d0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1db5135aa279908a48a6ead0189ba8396e708b772f52936786cd476f20d61d440f7e86e7e8db44f3005241921a50c0326c20ccb6ad14c4836e8cd704a824b09
|
7
|
+
data.tar.gz: f96293b488c0b9eba62d420869119e2c3e586def491532983782fc57862c923f7d66984c49a6dacef01f71c6f1fed6cc31a82d017de110b24516cf06d3c6bafe
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# minitest-stub-const
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/adammck/minitest-stub-const.svg)](https://travis-ci.org/adammck/minitest-stub-const)
|
4
|
+
|
3
5
|
Stub constants for the duration of a block in MiniTest.
|
4
6
|
Like RSpec's [stub_const] [rspec], but boring and non-magical.
|
5
7
|
|
data/lib/minitest/stub_const.rb
CHANGED
@@ -30,6 +30,24 @@ class Object
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
# Remove the constant +name+ for the duration of a block. This is
|
34
|
+
# useful when testing code that checks whether a constant is defined
|
35
|
+
# or not.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# Object.stub_remove_const(:File) do
|
40
|
+
# "Look ma, no File!" unless defined(File)
|
41
|
+
# end
|
42
|
+
# # => "Look ma, no File!"
|
43
|
+
def stub_remove_const(name)
|
44
|
+
orig = const_get(name)
|
45
|
+
remove_const(name)
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
const_set(name, orig)
|
49
|
+
end
|
50
|
+
|
33
51
|
# Add a minimal implementation of ActiveSupport's silence_warnings if it
|
34
52
|
# hasn't already been defined, to call a block with warnings disabled.
|
35
53
|
unless respond_to?(:silence_warnings)
|
data/test/test_stub_const.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require File.expand_path(
|
2
|
-
require
|
1
|
+
require File.expand_path('../../lib/minitest/stub_const', __FILE__)
|
2
|
+
require 'minitest/mock'
|
3
3
|
|
4
4
|
module A
|
5
5
|
module B
|
@@ -9,31 +9,43 @@ module A
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
describe
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
12
|
+
describe 'Object' do
|
13
|
+
describe '#stub_const' do
|
14
|
+
before do
|
15
|
+
@mock = MiniTest::Mock.new
|
16
|
+
@mock.expect(:what, :new)
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
it 'replaces a constant for the duration of a block' do
|
20
|
+
A.stub_const(:B, @mock) do
|
21
|
+
assert_equal :new, A::B.what
|
22
|
+
end
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
assert_equal :
|
25
|
+
it 'restores the original value after the block' do
|
26
|
+
A.stub_const(:B, @mock) { }
|
27
|
+
assert_equal :old, A::B.what
|
27
28
|
end
|
28
|
-
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
it 'does not raise any warnings' do
|
31
|
+
assert_silent { A.stub_const(:B, @mock) { } }
|
32
|
+
end
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
describe '#stub_remove_const' do
|
36
|
+
it 'removes a constant for the duration of a block' do
|
37
|
+
A.stub_remove_const(:B) do
|
38
|
+
refute defined?(A::B)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'restores the original value after the block' do
|
43
|
+
A.stub_remove_const(:B) { }
|
44
|
+
assert_equal :old, A::B.what
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'does not raise any warnings' do
|
48
|
+
assert_silent { A.stub_remove_const(:B) { } }
|
49
|
+
end
|
38
50
|
end
|
39
51
|
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.4'
|
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-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|