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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f69608945b256b31d1e44162a84fb46ee8f94252
4
- data.tar.gz: 681000bce0bfc30fbd5b1d047be419f1b01a249d
3
+ metadata.gz: 00edc8c44dd6ae8caa0d4b850178007b01eaf551
4
+ data.tar.gz: d77cedce7ae177bd65776fb0fef492f122239d0a
5
5
  SHA512:
6
- metadata.gz: 9c20539c125f69edbe09510c99e1c49b1d4bc790c6392848727bb444afe1f8f6fcfcdc8e7d7dfcfd2613618fe612f2541b4cf7c48108c28b056f03d6df72974d
7
- data.tar.gz: a0165f233b97e0dd56e51ffafc37031a8bcc5d602e01ef7e96bd981b5d713d8f1f86bc3cd474a8577068487b6b56410652a95ea9e0953e858c94c4e9c50f54ba
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
 
@@ -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)
@@ -1,5 +1,5 @@
1
- require File.expand_path("../../lib/minitest/stub_const", __FILE__)
2
- require "minitest/mock"
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 "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
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
- after do
21
- $stderr = @old_stderr
22
- end
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
- 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
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
- it "restores the original value after the block" do
31
- A.stub_const(:B, @mock) { }
32
- assert_equal :old, A::B.what
30
+ it 'does not raise any warnings' do
31
+ assert_silent { A.stub_const(:B, @mock) { } }
32
+ end
33
33
  end
34
34
 
35
- it "doesn't raise warnings" do
36
- A.stub_const(:B, @mock) { }
37
- assert_empty $stderr.string
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.3'
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-01-22 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: