mock_proc 0.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/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +6 -0
- data/lib/mock_proc.rb +14 -0
- data/lib/mock_proc/rspec.rb +9 -0
- data/lib/mock_proc/version.rb +5 -0
- data/mock_proc.gemspec +22 -0
- data/spec/mock_proc_spec.rb +48 -0
- data/spec/spec_helper.rb +13 -0
- metadata +136 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andy Lindeman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# MockProc
|
2
|
+
|
3
|
+
[](http://travis-ci.org/alindeman/mock_proc)
|
4
|
+
|
5
|
+
An attempt to create a mock `Proc` object, so expectations can be set on how
|
6
|
+
it is called.
|
7
|
+
|
8
|
+
Ideal for testing callbacks; alternatives are discussed in [Avdi's blog
|
9
|
+
post](http://devblog.avdi.org/2011/12/12/testing-that-a-block-is-called/).
|
10
|
+
|
11
|
+
`mock_proc` is meant to plug into other mocking libraries. Right now it
|
12
|
+
supports rspec-mocks.
|
13
|
+
|
14
|
+
## rspec-mocks
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
describe MockProc do
|
18
|
+
it "expects an exact number of calls" do
|
19
|
+
block = MockProc.new
|
20
|
+
block.should_be_called.twice
|
21
|
+
|
22
|
+
2.times { block.call }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
You can use all of the same predicates you expect:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
describe MockProc do
|
31
|
+
it "expects arguments" do
|
32
|
+
block = MockProc.new
|
33
|
+
block.should_be_called.with(:foo)
|
34
|
+
|
35
|
+
block.call(:foo)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
It works any way you invoke the block:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
describe MockProc do
|
44
|
+
it "allows all methods that invoke a block" do
|
45
|
+
block = MockProc.new
|
46
|
+
block.should_be_called.exactly(2).times
|
47
|
+
|
48
|
+
block[:foo]
|
49
|
+
block === :foo
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
## TODO
|
55
|
+
|
56
|
+
* Other mocking frameworks: mocha, ...?
|
data/Rakefile
ADDED
data/lib/mock_proc.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "delegate"
|
2
|
+
|
3
|
+
class MockProc < SimpleDelegator
|
4
|
+
def initialize
|
5
|
+
super(proc { |*a| __call__(*a) })
|
6
|
+
end
|
7
|
+
|
8
|
+
def __call__(*)
|
9
|
+
"not stubbed; are you sure you set an expectation?"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
require "mock_proc/version"
|
14
|
+
require "mock_proc/rspec"
|
data/mock_proc.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mock_proc/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Andy Lindeman"]
|
6
|
+
gem.email = ["alindeman@gmail.com"]
|
7
|
+
gem.description = %q{An object that looks like a proc that can be used to verify a block was called (elegantly, of course).}
|
8
|
+
gem.summary = %q{An object that looks like a proc that can be used to verify a block was called (elegantly, of course).}
|
9
|
+
gem.homepage = "https://github.com/alindeman/mock_proc"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mock_proc"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MockProc::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec", "~>2.10.0"
|
19
|
+
gem.add_development_dependency "bourne"
|
20
|
+
gem.add_development_dependency "pry"
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MockProc do
|
4
|
+
def call_block_with_yield
|
5
|
+
yield
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "rspec-mocks", "rspec-mocks" => true do
|
9
|
+
it "expects an exact number of calls" do
|
10
|
+
block = MockProc.new
|
11
|
+
block.should_be_called.twice
|
12
|
+
|
13
|
+
2.times { block.call }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "allows all methods that invoke a block" do
|
17
|
+
block = MockProc.new
|
18
|
+
block.should_be_called.exactly(2).times
|
19
|
+
|
20
|
+
block[:foo]
|
21
|
+
call_block_with_yield &block
|
22
|
+
end
|
23
|
+
|
24
|
+
if RUBY_VERSION >= '1.9'
|
25
|
+
it "allows the === method that invokes a block" do
|
26
|
+
block = MockProc.new
|
27
|
+
block.should_be_called
|
28
|
+
|
29
|
+
block === :foo
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "expects arguments" do
|
34
|
+
block = MockProc.new
|
35
|
+
block.should_be_called.with(:foo)
|
36
|
+
|
37
|
+
block.call(:foo)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "expects arguments (sad case)" do
|
41
|
+
block = MockProc.new
|
42
|
+
block.should_be_called.with(:bar)
|
43
|
+
|
44
|
+
expect { block.call(:foo) }.to raise_error
|
45
|
+
block.rspec_reset
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "pry"
|
2
|
+
require "rspec"
|
3
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "mock_proc")
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before "rspec-mocks" => true do
|
7
|
+
config.mock_with :rspec
|
8
|
+
end
|
9
|
+
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mock_proc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andy Lindeman
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-05-29 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
hash: 39
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 10
|
30
|
+
- 0
|
31
|
+
version: 2.10.0
|
32
|
+
prerelease: false
|
33
|
+
requirement: *id001
|
34
|
+
type: :development
|
35
|
+
name: rspec
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
prerelease: false
|
47
|
+
requirement: *id002
|
48
|
+
type: :development
|
49
|
+
name: bourne
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
prerelease: false
|
61
|
+
requirement: *id003
|
62
|
+
type: :development
|
63
|
+
name: pry
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
prerelease: false
|
75
|
+
requirement: *id004
|
76
|
+
type: :development
|
77
|
+
name: rake
|
78
|
+
description: An object that looks like a proc that can be used to verify a block was called (elegantly, of course).
|
79
|
+
email:
|
80
|
+
- alindeman@gmail.com
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files: []
|
86
|
+
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- .rspec
|
90
|
+
- .travis.yml
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/mock_proc.rb
|
96
|
+
- lib/mock_proc/rspec.rb
|
97
|
+
- lib/mock_proc/version.rb
|
98
|
+
- mock_proc.gemspec
|
99
|
+
- spec/mock_proc_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: https://github.com/alindeman/mock_proc
|
102
|
+
licenses: []
|
103
|
+
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
version: "0"
|
127
|
+
requirements: []
|
128
|
+
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.15
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: An object that looks like a proc that can be used to verify a block was called (elegantly, of course).
|
134
|
+
test_files:
|
135
|
+
- spec/mock_proc_spec.rb
|
136
|
+
- spec/spec_helper.rb
|