minitest-stub-const 0.1
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.
- data/lib/minitest/stub_const.rb +44 -0
- data/test/test_stub_const.rb +39 -0
- metadata +48 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
#
|
4
|
+
# Replace the +value+ of constant +name+ for the duration of a +block+. This
|
5
|
+
# is useful when testing that the expected class methods are being called on
|
6
|
+
# a Module or Class instance.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# m = MiniTest::Mock.new
|
11
|
+
# m.expect(:register, nil, [:whatever])
|
12
|
+
#
|
13
|
+
# MyLib.stub_const(:Thing, m) do
|
14
|
+
# @subject.add_thing(:whatever)
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# m.verify
|
18
|
+
#
|
19
|
+
def stub_const(name, val, &block)
|
20
|
+
orig = const_get(name)
|
21
|
+
|
22
|
+
silence_warnings do
|
23
|
+
const_set(name, val)
|
24
|
+
end
|
25
|
+
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
silence_warnings do
|
29
|
+
const_set(name, orig)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Add a minimal implementation of ActiveSupport's silence_warnings if it
|
34
|
+
# hasn't already been defined, to call a block with warnings disabled.
|
35
|
+
unless respond_to?(:silence_warnings)
|
36
|
+
def silence_warnings
|
37
|
+
orig = $VERBOSE
|
38
|
+
$VERBOSE = nil
|
39
|
+
yield
|
40
|
+
ensure
|
41
|
+
$VERBOSE = orig
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path("../../lib/minitest/stub_const", __FILE__)
|
2
|
+
require "minitest/mock"
|
3
|
+
|
4
|
+
module A
|
5
|
+
module B
|
6
|
+
def self.what
|
7
|
+
:old
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "Object" do
|
13
|
+
before do
|
14
|
+
@old_stderr = $stderr
|
15
|
+
$stderr = StringIO.new
|
16
|
+
@mock = MiniTest::Mock.new
|
17
|
+
@mock.expect(:what, :new)
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
$stderr = @old_stderr
|
22
|
+
end
|
23
|
+
|
24
|
+
it "replaces a constant for the duration of a block" do
|
25
|
+
A.stub_const(:B, @mock) do
|
26
|
+
assert_equal :new, A::B.what
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "restores the original value after the block" do
|
31
|
+
A.stub_const(:B, @mock) { }
|
32
|
+
assert_equal :old, A::B.what
|
33
|
+
end
|
34
|
+
|
35
|
+
it "doesn't raise warnings" do
|
36
|
+
A.stub_const(:B, @mock) { }
|
37
|
+
assert_empty $stderr.string
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-stub-const
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Mckaig
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
- adam.mckaig@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/minitest/stub_const.rb
|
22
|
+
- test/test_stub_const.rb
|
23
|
+
homepage: https://github.com/adammck/minitest-stub-const
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.24
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Stub constants for the duration of a block in MiniTest
|
47
|
+
test_files:
|
48
|
+
- test/test_stub_const.rb
|