mock_proxy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +31 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/mock_proxy.rb +199 -0
- data/lib/mock_proxy/version.rb +4 -0
- data/mock_proxy.gemspec +61 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0228b892195ffaa32f34dfb387245a871bde314
|
4
|
+
data.tar.gz: bbe3274ab88c596f0df31ad27544b723c5569d4a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2fcbbfe5ebe19d46846c0ec49d1b959f8f40262a093050afcfa4f99ab76ff799e6fda25f4627e5206dc1e8cef3323c4fc15a58932c0aab3aed9eec0a18e2d48
|
7
|
+
data.tar.gz: 9ee1cb4c96d9b808d8a328204f81e261c4da70d3b7a2872931bb4166669e0c0cb7eeeb0412a0dc1997d2714bffbda91f8ef48b171ab6e01970427ae41c4a3a44
|
data/.gitignore
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalization:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
/Gemfile.lock
|
29
|
+
.ruby-version
|
30
|
+
.ruby-gemset
|
31
|
+
.rvmrc
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 matrinox
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# MockProxy
|
2
|
+
|
3
|
+
A proxy that can be used to stub method chains and more!
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Remember when RSpec had `stub_chain`? They removed it for good reasons but sometimes you just need it.
|
8
|
+
Well, here it is, a proxy object. It doesn't actually mock anything for you (the name is just catchy) so you need to do that.
|
9
|
+
But that actually comes with a lot of benefits:
|
10
|
+
|
11
|
+
1. It's compatable with any testing framework
|
12
|
+
2. You can use it for purposes other than testing, e.g. prototyping, code stubs
|
13
|
+
3. Flexibility in how you use it without overloading the number of methods you have to remember
|
14
|
+
|
15
|
+
Here's an example usage:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
let(:model_proxy) do
|
19
|
+
MockProxy.new(email_client: {
|
20
|
+
create_email: {
|
21
|
+
receive: proc {}
|
22
|
+
}
|
23
|
+
})
|
24
|
+
end
|
25
|
+
before { allow(Model).to receive(:new).and_return model_proxy }
|
26
|
+
it 'should call receive' do
|
27
|
+
proc = MockProxy.get(model_proxy, 'email_client.create_email.receive')
|
28
|
+
expect(proc).to receive(:call)
|
29
|
+
run_system_under_test
|
30
|
+
MockProxy.update(mock_proxy, 'email_client.create_email.validate!') { true }
|
31
|
+
MockProxy.observe(mock_proxy, 'email_client.create_email.send') do |to|
|
32
|
+
expect(to).to eq 'stop@emailing.me'
|
33
|
+
end
|
34
|
+
run_system_under_test2
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
As you can see, the proc - which ends the proxy by calling the proc - can be used for anything. You can spy on the
|
39
|
+
call count and arguments, mock methods, or just stub out code you don't want executed. Because it doesn't make any
|
40
|
+
assumptions, it becomes very flexible. Simple, yet powerful, it's uses are infinite. Enjoy
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem 'mock_proxy'
|
48
|
+
```
|
49
|
+
|
50
|
+
And then execute:
|
51
|
+
|
52
|
+
$ bundle
|
53
|
+
|
54
|
+
Or install it yourself as:
|
55
|
+
|
56
|
+
$ gem install mock_proxy
|
57
|
+
|
58
|
+
## Usage
|
59
|
+
|
60
|
+
TODO: Write usage instructions here
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mock_proxy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
71
|
+
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "mock_proxy"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/mock_proxy.rb
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
require "mock_proxy/version"
|
2
|
+
|
3
|
+
# A non-opinionated proxy object that has multiple uses. It can be used for mocking, spying,
|
4
|
+
# stubbing. Use as a dummy, double, fake, etc. Every test double type possible. How? Let's see
|
5
|
+
#
|
6
|
+
# Example, say you want to stub this scenario: Model.new.generate_email.validate!.send(to: email)
|
7
|
+
# That would have be 5-6 lines of stubbing. If this sounds like stub_chain, you're on the right track. It was removed in
|
8
|
+
# RSpec 3 (or 2?). It's similar to that but it does things differently
|
9
|
+
# First, it doesn't require you to use it in a stub
|
10
|
+
# Second, it's use of procs means you can define anything, a stub or a mock (expectation) or a spy or whatever you want
|
11
|
+
#
|
12
|
+
# To use MockProxy, initialize it with a hash. Each key is a method call. Each call either returns a new proxy or calls
|
13
|
+
# the proc. If the value is a proc, it calls it immediately with the args and block. If the value is a hash, it returns
|
14
|
+
# a new proxy with the value as the hash. MockProxy will warn if you don't use hashes or procs and will also warn if
|
15
|
+
# you did not define all the method calls (it won't automatically return itself for methods not defined in the hash)
|
16
|
+
#
|
17
|
+
# Example use:
|
18
|
+
# let(:model_proxy) { MockProxy.new(generate_email: { validate!: { send: proc { |to| email } } }) }
|
19
|
+
# before { allow(Model).to receive(:new).and_return model_proxy }
|
20
|
+
# # ...
|
21
|
+
# describe 'Model' do
|
22
|
+
# it 'model also receives email' do
|
23
|
+
# callback = proc { |message| expect(message).to eq 'message' }
|
24
|
+
# MockProxy.update_proxy(model_proxy, receive_email: callback)
|
25
|
+
# run_system_under_test
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# NOTE: You don't have to use only one mock proxy for all calls. You can break it up if you want to have more control
|
30
|
+
# over each method call
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
# let(:model_proxy) do
|
34
|
+
# callback = proc do |type|
|
35
|
+
# MockProxy.update_proxy(generator_proxy, decorate: proc { |*args| method_call(type, *args) })
|
36
|
+
# generator_proxy
|
37
|
+
# end
|
38
|
+
# MockProxy.new(generate_email: callback)
|
39
|
+
# end
|
40
|
+
# let(:generator_proxy) { MockProxy.new(validate!: { send: proc { |to| email } }) }
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# @author Geoff Lee
|
44
|
+
# @since 0.1.0
|
45
|
+
#
|
46
|
+
class MockProxy
|
47
|
+
# Deep merges the callback tree, replacing existing values with new values
|
48
|
+
#
|
49
|
+
# Use case: Reuse existing stub but with some different values
|
50
|
+
#
|
51
|
+
# @param [MockProxy] proxy existing proxy
|
52
|
+
# @param [Hash] new_callback_hash new partial callback tree
|
53
|
+
# @return [MockProxy] the original proxy object
|
54
|
+
def self.merge(proxy, new_callback_hash)
|
55
|
+
existing_callback_hash = proxy.instance_variable_get('@callback_hash')
|
56
|
+
new_callback_hash = new_callback_hash.deep_stringify_keys
|
57
|
+
new_callback_hash = existing_callback_hash.deep_merge(new_callback_hash).freeze
|
58
|
+
proxy.instance_variable_set('@callback_hash', new_callback_hash)
|
59
|
+
proxy
|
60
|
+
end
|
61
|
+
|
62
|
+
# Replaces the proc at the specified key path
|
63
|
+
#
|
64
|
+
# Use case: Reuse existing stub but modify a proc
|
65
|
+
#
|
66
|
+
# @param [MockProxy] proxy existing proxy
|
67
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
68
|
+
# dot delimited key path or an array of method names as strings or symbols
|
69
|
+
# @return [MockProxy] the original proxy object
|
70
|
+
def self.replace_at(proxy, key_path, &block)
|
71
|
+
set_callback(proxy, key_path, block)
|
72
|
+
proxy
|
73
|
+
end
|
74
|
+
|
75
|
+
# Add an observer to an existing proxy
|
76
|
+
#
|
77
|
+
# Use case: Observe method call without changing the existing callback's stubbed return value
|
78
|
+
#
|
79
|
+
# @param [MockProxy] proxy existing proxy
|
80
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
81
|
+
# dot delimited key path or an array of method names as strings or symbols
|
82
|
+
# @yieldparam [*args] args
|
83
|
+
# @yieldreturn [optional]
|
84
|
+
# @return [MockProxy] the original proxy object
|
85
|
+
def self.observe(proxy, key_path, &block)
|
86
|
+
callback = get_callback(proxy, key_path)
|
87
|
+
# Wrap existing callback, calling the provided block before it
|
88
|
+
# Multiple calls to .observe will create a pyramid of callbacks, calling the observers before
|
89
|
+
# eventually calling the existing callback
|
90
|
+
new_callback = proc do |*args|
|
91
|
+
block.call(*args)
|
92
|
+
callback.call(*args)
|
93
|
+
end
|
94
|
+
set_callback(proxy, key_path, new_callback)
|
95
|
+
proxy
|
96
|
+
end
|
97
|
+
|
98
|
+
# Wraps the existing callback with your block
|
99
|
+
#
|
100
|
+
# Use case: Get full control of the existing proc while running custom code
|
101
|
+
#
|
102
|
+
# @param [MockProxy] proxy existing proxy
|
103
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
104
|
+
# dot delimited key path or an array of method names as strings or symbols
|
105
|
+
# @yieldparam [*args, &block] args, original callback
|
106
|
+
# @yieldreturn [optional]
|
107
|
+
# @return [MockProxy] the original proxy object
|
108
|
+
def self.wrap(proxy, key_path, &block)
|
109
|
+
callback = get_callback(proxy, key_path)
|
110
|
+
# Wrap existing callback, calling the provided block before it
|
111
|
+
# Multiple calls to .observe will create a pyramid of callbacks, calling the observers before
|
112
|
+
# eventually calling the existing callback
|
113
|
+
new_callback = proc do |*args|
|
114
|
+
block.call(*args, &callback)
|
115
|
+
end
|
116
|
+
set_callback(proxy, key_path, new_callback)
|
117
|
+
proxy
|
118
|
+
end
|
119
|
+
|
120
|
+
# @private
|
121
|
+
# @param [MockProxy] proxy existing proxy
|
122
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
123
|
+
# dot delimited key path or an array of method names as strings or symbols
|
124
|
+
# @return [Proc] if proc found at key path
|
125
|
+
# @raise [ArgumentError] if proc not found or hash found at key path
|
126
|
+
def self.get_callback(proxy, key_path)
|
127
|
+
key_paths = key_path.is_a?(Array) ? key_path.map(&:to_s) :key_path.split('.')
|
128
|
+
existing_callback_hash = proxy.instance_variable_get('@callback_hash')
|
129
|
+
callback = key_paths.reduce(existing_callback_hash) do |callback_hash, key|
|
130
|
+
if callback_hash && callback_hash[key]
|
131
|
+
callback_hash[key]
|
132
|
+
else
|
133
|
+
fail ArgumentError, "The existing callback tree does not contain the full key path you provided. We stopped at #{key} and the callback tree looks like this: #{existing_callback_hash}"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
if callback.is_a?(Proc)
|
137
|
+
callback
|
138
|
+
else
|
139
|
+
fail ArgumentError, "The existing callback tree contains the full key path you provided but continues going. If you want to shorten the callback tree, use MockProxy.update. The callback tree looks like this: #{existing_callback_hash}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
private_class_method :get_callback
|
143
|
+
|
144
|
+
# @private
|
145
|
+
# @param [MockProxy] proxy existing proxy
|
146
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
147
|
+
# dot delimited key path or an array of method names as strings or symbols
|
148
|
+
# @param [Proc] proc the new proc to replace the existing proc
|
149
|
+
# @return [MockProxy] if proc existed at key path
|
150
|
+
# @raise [ArgumentError] if proc not found or hash found at key path
|
151
|
+
def self.set_callback(proxy, key_path, proc)
|
152
|
+
key_paths = key_path.is_a?(Array) ? key_path.map(&:to_s) :key_path.split('.')
|
153
|
+
copied_callback_hash = proxy.instance_variable_get('@callback_hash').clone
|
154
|
+
key_paths.reduce(copied_callback_hash) do |callback_hash, key|
|
155
|
+
if !callback_hash || !callback_hash[key]
|
156
|
+
fail ArgumentError, "The existing callback tree does not contain the full key path you provided. We stopped at #{key} and the callback tree looks like this: #{existing_callback_hash}"
|
157
|
+
end
|
158
|
+
if callback_hash[key].is_a?(Proc)
|
159
|
+
callback_hash[key] = proc
|
160
|
+
else
|
161
|
+
callback_hash[key]
|
162
|
+
end
|
163
|
+
end
|
164
|
+
proxy.instance_variable_set('@callback_hash', copied_callback_hash)
|
165
|
+
end
|
166
|
+
private_class_method :set_callback
|
167
|
+
|
168
|
+
# Retrieve the existing callback or callback tree at the specified key path
|
169
|
+
#
|
170
|
+
# NOTE: We freeze the hash so you cannot modify it
|
171
|
+
#
|
172
|
+
# Use case: Retrieve proc to mock or retrieve hash to reflect on what is currently being stubbed
|
173
|
+
#
|
174
|
+
# @param [MockProxy] proxy existing proxy
|
175
|
+
# @param [String, Array<String>] key_path the chain of methods or key path. Can be a
|
176
|
+
# dot delimited key path or an array of method names as strings or symbols
|
177
|
+
# @return [Block, Hash]
|
178
|
+
def self.get(proxy, key_path)
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
# @param [Hash] callback_hash the tree of chained method calls
|
183
|
+
def initialize(callback_hash)
|
184
|
+
@callback_hash = callback_hash.deep_stringify_keys.freeze
|
185
|
+
end
|
186
|
+
|
187
|
+
# @private
|
188
|
+
def method_missing(name, *args, &block)
|
189
|
+
current = @callback_hash[name.to_s]
|
190
|
+
if current.is_a?(Proc)
|
191
|
+
current.call(*args, &block)
|
192
|
+
else
|
193
|
+
if !current.is_a?(Proc) && !current.is_a?(Hash)
|
194
|
+
fail "Missing method #{name}. Please add this definition to your mock proxy"
|
195
|
+
end
|
196
|
+
MockProxy.new(current.freeze)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
data/mock_proxy.gemspec
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mock_proxy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mock_proxy"
|
8
|
+
spec.version = MockProxy::VERSION
|
9
|
+
spec.authors = ["matrinox"]
|
10
|
+
spec.email = ["geofflee25@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A proxy that can be used to stub method chains and more!"
|
13
|
+
spec.description = "Remember when RSpec had stub_chain? They removed it for good reasons but sometimes you just need it.
|
14
|
+
Well, here it is, a proxy object. It doesn't actually mock anything for you (the name is just catchy) so you need to do that.
|
15
|
+
But that actually comes with a lot of benefits:
|
16
|
+
1) It's compatable with any testing framework
|
17
|
+
2) You can use it for purposes other than testing, e.g. prototyping, code stubs
|
18
|
+
3) Flexibility in how you use it without overloading the number of methods you have to remember
|
19
|
+
|
20
|
+
Here's an example usage:
|
21
|
+
let(:model_proxy) do
|
22
|
+
MockProxy.new(email_client: {
|
23
|
+
create_email: {
|
24
|
+
receive: proc {}
|
25
|
+
}
|
26
|
+
})
|
27
|
+
end
|
28
|
+
before { allow(Model).to receive(:new).and_return model_proxy }
|
29
|
+
it 'should call receive' do
|
30
|
+
proc = MockProxy.get(model_proxy, 'email_client.create_email.receive')
|
31
|
+
expect(proc).to receive(:call)
|
32
|
+
run_system_under_test
|
33
|
+
MockProxy.update(mock_proxy, 'email_client.create_email.validate!') { true }
|
34
|
+
MockProxy.observe(mock_proxy, 'email_client.create_email.send') do |to|
|
35
|
+
expect(to).to eq 'stop@emailing.me'
|
36
|
+
end
|
37
|
+
run_system_under_test2
|
38
|
+
end
|
39
|
+
|
40
|
+
As you can see, the proc - which ends the proxy by calling the proc - can be used for anything. You can spy on the
|
41
|
+
call count and arguments, mock methods, or just stub out code you don't want executed. Because it doesn't make any
|
42
|
+
assumptions, it becomes very flexible. Simple, yet powerful, it's uses are infinite. Enjoy"
|
43
|
+
spec.homepage = "https://github.com/matrinox/MockProxy"
|
44
|
+
spec.license = "MIT"
|
45
|
+
|
46
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
47
|
+
# delete this section to allow pushing this gem to any host.
|
48
|
+
if spec.respond_to?(:metadata)
|
49
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
50
|
+
else
|
51
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
52
|
+
end
|
53
|
+
|
54
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
55
|
+
spec.bindir = "exe"
|
56
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
57
|
+
spec.require_paths = ["lib"]
|
58
|
+
|
59
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
60
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mock_proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- matrinox
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |-
|
42
|
+
Remember when RSpec had stub_chain? They removed it for good reasons but sometimes you just need it.
|
43
|
+
Well, here it is, a proxy object. It doesn't actually mock anything for you (the name is just catchy) so you need to do that.
|
44
|
+
But that actually comes with a lot of benefits:
|
45
|
+
1) It's compatable with any testing framework
|
46
|
+
2) You can use it for purposes other than testing, e.g. prototyping, code stubs
|
47
|
+
3) Flexibility in how you use it without overloading the number of methods you have to remember
|
48
|
+
|
49
|
+
Here's an example usage:
|
50
|
+
let(:model_proxy) do
|
51
|
+
MockProxy.new(email_client: {
|
52
|
+
create_email: {
|
53
|
+
receive: proc {}
|
54
|
+
}
|
55
|
+
})
|
56
|
+
end
|
57
|
+
before { allow(Model).to receive(:new).and_return model_proxy }
|
58
|
+
it 'should call receive' do
|
59
|
+
proc = MockProxy.get(model_proxy, 'email_client.create_email.receive')
|
60
|
+
expect(proc).to receive(:call)
|
61
|
+
run_system_under_test
|
62
|
+
MockProxy.update(mock_proxy, 'email_client.create_email.validate!') { true }
|
63
|
+
MockProxy.observe(mock_proxy, 'email_client.create_email.send') do |to|
|
64
|
+
expect(to).to eq 'stop@emailing.me'
|
65
|
+
end
|
66
|
+
run_system_under_test2
|
67
|
+
end
|
68
|
+
|
69
|
+
As you can see, the proc - which ends the proxy by calling the proc - can be used for anything. You can spy on the
|
70
|
+
call count and arguments, mock methods, or just stub out code you don't want executed. Because it doesn't make any
|
71
|
+
assumptions, it becomes very flexible. Simple, yet powerful, it's uses are infinite. Enjoy
|
72
|
+
email:
|
73
|
+
- geofflee25@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".gitignore"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/mock_proxy.rb
|
88
|
+
- lib/mock_proxy/version.rb
|
89
|
+
- mock_proxy.gemspec
|
90
|
+
homepage: https://github.com/matrinox/MockProxy
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
allowed_push_host: https://rubygems.org
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.5
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A proxy that can be used to stub method chains and more!
|
115
|
+
test_files: []
|