spy 0.2.2 → 0.2.3
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/Gemfile +0 -2
- data/README.md +21 -5
- data/lib/spy.rb +11 -17
- data/lib/spy/core_ext/marshal.rb +3 -4
- data/lib/spy/subroutine.rb +10 -8
- data/lib/spy/version.rb +1 -1
- data/spy.gemspec +7 -3
- metadata +62 -18
- checksums.yaml +0 -7
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
# Spy
|
1
|
+
# Spy [](https://travis-ci.org/ryanong/spy) [](http://badge.fury.io/rb/spy)
|
2
|
+
|
3
|
+
[Docs](http://rdoc.info/gems/spy/frames)
|
2
4
|
|
3
5
|
Spy is a lightweight stubbing framework with support for method spies, constant stubs, and object doubles.
|
4
6
|
|
@@ -35,8 +37,9 @@ Fail faster, code faster.
|
|
35
37
|
* cannot transfer nested constants when stubbing a constant
|
36
38
|
* i don't think anybody uses this anyway
|
37
39
|
* nobody on github does
|
38
|
-
* #with is not supported
|
39
|
-
*
|
40
|
+
* #with is not supported
|
41
|
+
* you can usually just check the call logs.
|
42
|
+
* if you do need to use this. It is probably a code smell. You either need to abstract your method more or add separate tests.
|
40
43
|
|
41
44
|
## Installation
|
42
45
|
|
@@ -69,7 +72,7 @@ book.title #=> "East of Eden"
|
|
69
72
|
```
|
70
73
|
|
71
74
|
Spy will raise an error if you try to stub on a method that doesn't exist.
|
72
|
-
You can force the creation of a
|
75
|
+
You can force the creation of a stub on method that didn't exist but it really isn't suggested.
|
73
76
|
|
74
77
|
```ruby
|
75
78
|
Spy.new(book, :flamethrower).hook(force:true).and_return("burnninante")
|
@@ -126,9 +129,16 @@ When you stub a method it returns a spy. A spy records what calls have been made
|
|
126
129
|
validator = Spy.double("validator")
|
127
130
|
validate_spy = Spy.on(validator, :validate)
|
128
131
|
validate_spy.has_been_called? #=> false
|
132
|
+
|
129
133
|
validator.validate("01234") #=> nil
|
130
134
|
validate_spy.has_been_called? #=> true
|
131
|
-
validate_spy.has_been_called_with?("01234) #=> true
|
135
|
+
validate_spy.has_been_called_with?("01234") #=> true
|
136
|
+
```
|
137
|
+
|
138
|
+
You can also retrieve a method spy on demand
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
Spy.get(validator, :validate)
|
132
142
|
```
|
133
143
|
|
134
144
|
### Calling through
|
@@ -138,6 +148,8 @@ If you just want to make sure if a method is called and not override the output
|
|
138
148
|
Spy.on(book, :read_page).and_call_through
|
139
149
|
```
|
140
150
|
|
151
|
+
By if the original method never existed it will call #method\_missing on the spied object.
|
152
|
+
|
141
153
|
### Call Logs
|
142
154
|
|
143
155
|
When a spy is called on it records a call log. A call log contains the object it was called on, the arguments and block that were sent to method and what it returned.
|
@@ -153,6 +165,7 @@ first_call.object #=> book
|
|
153
165
|
first_call.args #=> [5]
|
154
166
|
first_call.block #=> Proc.new { "this is a block" }
|
155
167
|
first_call.result #=> "hello world"
|
168
|
+
first_call.called_from #=> "file_name.rb:line_number"
|
156
169
|
```
|
157
170
|
|
158
171
|
### MiniTest
|
@@ -171,6 +184,7 @@ require "rspec/autorun"
|
|
171
184
|
require "spy"
|
172
185
|
RSpec.configure do |c|
|
173
186
|
c.after { Spy.teardown }
|
187
|
+
c.mock_with :absolutely_nothing # this is completely optional.
|
174
188
|
end
|
175
189
|
```
|
176
190
|
|
@@ -180,6 +194,8 @@ end
|
|
180
194
|
require "spy"
|
181
195
|
class Test::Unit::TestCase
|
182
196
|
def teardown
|
197
|
+
# if you don't add super to every teardown then you will have to add this
|
198
|
+
# line to every file.
|
183
199
|
Spy.teardown
|
184
200
|
end
|
185
201
|
end
|
data/lib/spy.rb
CHANGED
@@ -32,7 +32,7 @@ module Spy
|
|
32
32
|
if spy
|
33
33
|
spy.unhook
|
34
34
|
else
|
35
|
-
raise "
|
35
|
+
raise "#{spy.inspect} was not found to be hooked"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -54,7 +54,7 @@ module Spy
|
|
54
54
|
if spy
|
55
55
|
spy.unhook
|
56
56
|
else
|
57
|
-
raise "
|
57
|
+
raise "#{spy.inspect} was not found to be hooked"
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -72,14 +72,14 @@ module Spy
|
|
72
72
|
end
|
73
73
|
spies = constant_names.map do |constant_name|
|
74
74
|
case constant_name
|
75
|
-
when
|
75
|
+
when Symbol
|
76
76
|
Constant.on(base_module, constant_name)
|
77
77
|
when Hash
|
78
78
|
constant_name.map do |name, result|
|
79
|
-
|
79
|
+
Constant.on(base_module, name).and_return(result)
|
80
80
|
end
|
81
81
|
else
|
82
|
-
raise ArgumentError.new "#{constant_name.class} is an invalid input, #on only accepts
|
82
|
+
raise ArgumentError.new "#{constant_name.class} is an invalid input, #on only accepts Symbol, and Hash"
|
83
83
|
end
|
84
84
|
end.flatten
|
85
85
|
|
@@ -91,23 +91,17 @@ module Spy
|
|
91
91
|
# @param constant_names *[Symbol]
|
92
92
|
# @return [Constant, Array<Constant>]
|
93
93
|
def off_const(base_module, *constant_names)
|
94
|
-
if base_module.is_a?(
|
94
|
+
if base_module.is_a?(Symbol)
|
95
95
|
constant_names.unshift(base_module)
|
96
96
|
base_module = Object
|
97
97
|
end
|
98
98
|
|
99
99
|
spies = constant_names.map do |constant_name|
|
100
|
-
|
101
|
-
|
102
|
-
Constant.off(base_module, constant_name)
|
103
|
-
when Hash
|
104
|
-
constant_name.map do |name, result|
|
105
|
-
off_const(base_module, name).and_return(result)
|
106
|
-
end
|
107
|
-
else
|
108
|
-
raise ArgumentError.new "#{constant_name.class} is an invalid input, #on only accepts String, Symbol, and Hash"
|
100
|
+
unless constant_name.is_a?(Symbol)
|
101
|
+
raise ArgumentError.new "#{constant_name.class} is an invalid input, #on only accepts Symbol, and Hash"
|
109
102
|
end
|
110
|
-
|
103
|
+
Constant.off(base_module, constant_name)
|
104
|
+
end
|
111
105
|
|
112
106
|
spies.size > 1 ? spies : spies.first
|
113
107
|
end
|
@@ -140,7 +134,7 @@ module Spy
|
|
140
134
|
# @param constant_names *[Symbol]
|
141
135
|
# @return [Constant, Array<Constant>]
|
142
136
|
def get_const(base_module, *constant_names)
|
143
|
-
if base_module.is_a?(
|
137
|
+
if base_module.is_a?(Symbol)
|
144
138
|
constant_names.unshift(base_module)
|
145
139
|
base_module = Object
|
146
140
|
end
|
data/lib/spy/core_ext/marshal.rb
CHANGED
@@ -9,15 +9,14 @@ module Marshal
|
|
9
9
|
end
|
10
10
|
|
11
11
|
spy_hook_options = spies.map do |spy|
|
12
|
-
|
13
|
-
[spy.unhook, opts]
|
12
|
+
[spy.hook_opts, spy.unhook]
|
14
13
|
end
|
15
14
|
|
16
15
|
begin
|
17
16
|
dump_without_mocks(*args.unshift(object.dup))
|
18
17
|
ensure
|
19
|
-
spy_hook_options.each do |
|
20
|
-
spy.hook(
|
18
|
+
spy_hook_options.each do |hook_opts, spy|
|
19
|
+
spy.hook(hook_opts)
|
21
20
|
end
|
22
21
|
end
|
23
22
|
end
|
data/lib/spy/subroutine.rb
CHANGED
@@ -305,14 +305,16 @@ module Spy
|
|
305
305
|
# @param singleton_method [Boolean] (true) only get singleton_method_spies
|
306
306
|
# @return [Array<Subroutine>]
|
307
307
|
def get_spies(base_object, singleton_methods = true)
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
base_object.
|
315
|
-
|
308
|
+
all_methods =
|
309
|
+
if singleton_methods
|
310
|
+
base_object.public_methods(false) +
|
311
|
+
base_object.protected_methods(false) +
|
312
|
+
base_object.private_methods(false)
|
313
|
+
else
|
314
|
+
base_object.public_instance_methods(false) +
|
315
|
+
base_object.protected_instance_methods(false) +
|
316
|
+
base_object.private_instance_methods(false)
|
317
|
+
end
|
316
318
|
|
317
319
|
all_methods.map do |method_name|
|
318
320
|
Agency.instance.find(get_spy_id(base_object.method(method_name)))
|
data/lib/spy/version.rb
CHANGED
data/spy.gemspec
CHANGED
@@ -6,16 +6,20 @@ require 'spy/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "spy"
|
8
8
|
gem.version = Spy::VERSION
|
9
|
+
gem.required_ruby_version = '>= 1.9.3'
|
10
|
+
gem.license = 'MIT'
|
9
11
|
gem.authors = ["Ryan Ong"]
|
10
12
|
gem.email = ["ryanong@gmail.com"]
|
11
|
-
gem.description = %q{A simple mocking library that
|
12
|
-
gem.summary = %q{
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.description = %q{A simple modern mocking library that uses the spy pattern and checks method's existence and arity.}
|
14
|
+
gem.summary = %q{Spy is a mocking library that was made for the modern age. It supports only 1.9.3+. Spy by default will raise an error if you attempt to stub a method that doesn't exist or call the stubbed method with the wrong arity.}
|
15
|
+
gem.homepage = "https://github.com/ryanong/spy"
|
14
16
|
|
15
17
|
gem.files = `git ls-files`.split($/)
|
16
18
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
19
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
20
|
gem.require_paths = ["lib"]
|
21
|
+
gem.add_development_dependency('pry')
|
22
|
+
gem.add_development_dependency('pry-nav')
|
19
23
|
gem.add_development_dependency('minitest', '>= 4.5.0')
|
20
24
|
gem.add_development_dependency('rspec-core')
|
21
25
|
gem.add_development_dependency('rspec-expectations')
|
metadata
CHANGED
@@ -1,58 +1,98 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Ryan Ong
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-28 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pry
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry-nav
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
13
46
|
- !ruby/object:Gem::Dependency
|
14
47
|
name: minitest
|
15
48
|
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
16
50
|
requirements:
|
17
|
-
- - '>='
|
51
|
+
- - ! '>='
|
18
52
|
- !ruby/object:Gem::Version
|
19
53
|
version: 4.5.0
|
20
54
|
type: :development
|
21
55
|
prerelease: false
|
22
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
23
58
|
requirements:
|
24
|
-
- - '>='
|
59
|
+
- - ! '>='
|
25
60
|
- !ruby/object:Gem::Version
|
26
61
|
version: 4.5.0
|
27
62
|
- !ruby/object:Gem::Dependency
|
28
63
|
name: rspec-core
|
29
64
|
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
30
66
|
requirements:
|
31
|
-
- - '>='
|
67
|
+
- - ! '>='
|
32
68
|
- !ruby/object:Gem::Version
|
33
69
|
version: '0'
|
34
70
|
type: :development
|
35
71
|
prerelease: false
|
36
72
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
37
74
|
requirements:
|
38
|
-
- - '>='
|
75
|
+
- - ! '>='
|
39
76
|
- !ruby/object:Gem::Version
|
40
77
|
version: '0'
|
41
78
|
- !ruby/object:Gem::Dependency
|
42
79
|
name: rspec-expectations
|
43
80
|
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
44
82
|
requirements:
|
45
|
-
- - '>='
|
83
|
+
- - ! '>='
|
46
84
|
- !ruby/object:Gem::Version
|
47
85
|
version: '0'
|
48
86
|
type: :development
|
49
87
|
prerelease: false
|
50
88
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
51
90
|
requirements:
|
52
|
-
- - '>='
|
91
|
+
- - ! '>='
|
53
92
|
- !ruby/object:Gem::Version
|
54
93
|
version: '0'
|
55
|
-
description: A simple mocking library that
|
94
|
+
description: A simple modern mocking library that uses the spy pattern and checks
|
95
|
+
method's existence and arity.
|
56
96
|
email:
|
57
97
|
- ryanong@gmail.com
|
58
98
|
executables: []
|
@@ -100,29 +140,33 @@ files:
|
|
100
140
|
- test/spy/test_subroutine.rb
|
101
141
|
- test/support/pen.rb
|
102
142
|
- test/test_helper.rb
|
103
|
-
homepage:
|
104
|
-
licenses:
|
105
|
-
|
143
|
+
homepage: https://github.com/ryanong/spy
|
144
|
+
licenses:
|
145
|
+
- MIT
|
106
146
|
post_install_message:
|
107
147
|
rdoc_options: []
|
108
148
|
require_paths:
|
109
149
|
- lib
|
110
150
|
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
111
152
|
requirements:
|
112
|
-
- - '>='
|
153
|
+
- - ! '>='
|
113
154
|
- !ruby/object:Gem::Version
|
114
|
-
version:
|
155
|
+
version: 1.9.3
|
115
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
157
|
+
none: false
|
116
158
|
requirements:
|
117
|
-
- - '>='
|
159
|
+
- - ! '>='
|
118
160
|
- !ruby/object:Gem::Version
|
119
161
|
version: '0'
|
120
162
|
requirements: []
|
121
163
|
rubyforge_project:
|
122
|
-
rubygems_version:
|
164
|
+
rubygems_version: 1.8.23
|
123
165
|
signing_key:
|
124
|
-
specification_version:
|
125
|
-
summary:
|
166
|
+
specification_version: 3
|
167
|
+
summary: Spy is a mocking library that was made for the modern age. It supports only
|
168
|
+
1.9.3+. Spy by default will raise an error if you attempt to stub a method that
|
169
|
+
doesn't exist or call the stubbed method with the wrong arity.
|
126
170
|
test_files:
|
127
171
|
- spec/spec_helper.rb
|
128
172
|
- spec/spy/and_call_original_spec.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 25f96e1254110c34d4b25ace27db39cf4933cc2a
|
4
|
-
data.tar.gz: 84320005a4c47158b70dc86fca661f6ae95f6ad6
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: 340277d830dfb87085d61fb443b834cb6636e8dda7b5d2af0166aab6835918747343849efed3b56032071d5d44b49cebaca0edb04df0c7997c10006cdb6cc897
|
7
|
-
data.tar.gz: 2b147cc08e3612270d8f18789bc4116621334e6da864bb3299b79ce857247513c467e4bf7f27e8c0354b064801445b392856489758fd6456015d44000dad987d
|