minitest-stub_any_instance 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/minitest/stub_any_instance.rb +25 -0
- data/test/stub_any_instance_test.rb +36 -0
- metadata +72 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
class Object
|
2
|
+
def self.stub_any_instance name, val_or_callable, &block
|
3
|
+
new_name = "__minitest_any_instance_stub__#{name}"
|
4
|
+
|
5
|
+
class_eval do
|
6
|
+
alias_method new_name, name
|
7
|
+
|
8
|
+
define_method(name) do |*args|
|
9
|
+
if val_or_callable.respond_to? :call then
|
10
|
+
val_or_callable.call(*args)
|
11
|
+
else
|
12
|
+
val_or_callable
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
class_eval do
|
20
|
+
undef_method name
|
21
|
+
alias_method name, new_name
|
22
|
+
undef_method new_name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'stringio'
|
3
|
+
require File.expand_path("../../lib/minitest/stub_any_instance", __FILE__)
|
4
|
+
|
5
|
+
class TestStubAnyInstance < MiniTest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
$stderr = StringIO.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_stubs_a_method_for_the_duration_of_a_block
|
11
|
+
got_here = false
|
12
|
+
String.stub_any_instance(:length, 42) do
|
13
|
+
assert_equal "hello".length, 42
|
14
|
+
got_here = true
|
15
|
+
end
|
16
|
+
assert got_here
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_restores_the_original_method_after_the_block
|
20
|
+
string = "hello world"
|
21
|
+
got_here = false
|
22
|
+
String.stub_any_instance(:length, 42) do
|
23
|
+
assert_equal string.length, 42
|
24
|
+
got_here = true
|
25
|
+
end
|
26
|
+
assert_equal string.length, 11
|
27
|
+
assert got_here
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_does_not_raise_warnings
|
31
|
+
got_here = false
|
32
|
+
String.stub_any_instance(:length, 42) { got_here = true }
|
33
|
+
assert_empty $stderr.string
|
34
|
+
assert got_here
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-stub_any_instance
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sammy Larbi
|
9
|
+
- Vasiliy Ermolovich
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-09-22 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: &2166611180 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '1.3'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2166611180
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &2166610720 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2166610720
|
37
|
+
description: Adds a method to MiniTest that stubs any instance of a class.
|
38
|
+
email:
|
39
|
+
- sam@codeodor.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- lib/minitest/stub_any_instance.rb
|
45
|
+
- test/stub_any_instance_test.rb
|
46
|
+
homepage: https://github.com/codeodor/minitest-stub_any_instance
|
47
|
+
licenses:
|
48
|
+
- MIT
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.8
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: ''
|
71
|
+
test_files:
|
72
|
+
- test/stub_any_instance_test.rb
|