minitest-moar 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +2 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +145 -0
- data/Rakefile +14 -0
- data/lib/minitest/moar.rb +12 -0
- data/lib/minitest/moar/assertions.rb +39 -0
- data/lib/minitest/moar/stubbing.rb +52 -0
- data/lib/minitest/moar/version.rb +5 -0
- data/minitest-moar.gemspec +23 -0
- data/test/spy_test.rb +75 -0
- data/test/stubbing_test.rb +42 -0
- data/test/test_helper.rb +17 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe8bbfa6c98e82a19959a49facd10ac9f874ea9a
|
4
|
+
data.tar.gz: 5f7e90bc5c9564bbdfa5f125d111f54165d34de6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef1f32afaaf4b46a6609ca2c5d7d8a556191ab01e982b5884cfa907fae3379c3002acaf843ee2eb1abcb4b3c47e8e092f6ee2a0170309426e16379dbc6df7297
|
7
|
+
data.tar.gz: 2237d031ee26e2ae12d2b635e502c486bc6cf46749a6638ce93d4cc85573e185d8ccf5982b7a1c5503994661c8763c4dbd6c15be1e66c2e998a6f2abbd852086
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brian Cardarella
|
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,145 @@
|
|
1
|
+
# Minitest Moar #
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/dockyard/minitest-moar.png?branch=master)](http://travis-ci.org/dockyard/minitest-moar)
|
4
|
+
[![Dependency Status](https://gemnasium.com/dockyard/minitest-moar.png?travis)](https://gemnasium.com/dockyard/minitest-moar)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/dockyard/minitest-moar.png)](https://codeclimate.com/github/dockyard/minitest-moar)
|
6
|
+
|
7
|
+
![Moar!](http://i.imgur.com/HfsSZV3.gif)
|
8
|
+
|
9
|
+
Some changes to Minitest and additional features
|
10
|
+
|
11
|
+
## Looking for help? ##
|
12
|
+
|
13
|
+
If it is a bug [please open an issue on
|
14
|
+
GitHub](https://github.com/dockyard/minitest-moar/issues).
|
15
|
+
|
16
|
+
## About
|
17
|
+
|
18
|
+
### Stub
|
19
|
+
|
20
|
+
By default Minitest pollutes `Object` with `Object#stub`. This gem
|
21
|
+
removes `Object#stub` and relaces with a method that is intended to be
|
22
|
+
used:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
stub Book, :read, true do
|
26
|
+
# do your thing
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
No moar object pollution!
|
31
|
+
|
32
|
+
### Instance stubbing
|
33
|
+
|
34
|
+
Minitest doesn't come with any way to stub the instance of a class.
|
35
|
+
Minitest Moar does:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
instance_stub Person, :say, "hello" do
|
39
|
+
person = Person.new
|
40
|
+
|
41
|
+
# this is the stubbed method
|
42
|
+
person.say
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
### Test Spies
|
47
|
+
|
48
|
+
**Note that test spies only work on stubbed methods.**
|
49
|
+
|
50
|
+
A common pattern might be to confirm if an object calls a method. You
|
51
|
+
can current do this with stubbing:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
@called = 0
|
55
|
+
caller = Proc.new { @called += 1 }
|
56
|
+
stub Book, :read, caller do
|
57
|
+
Book.read
|
58
|
+
end
|
59
|
+
assert_equal 1, @called
|
60
|
+
```
|
61
|
+
|
62
|
+
With Minitest Moar you get test spies in the form of `assert_called`:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
stub Book, :read do
|
66
|
+
Book.read
|
67
|
+
end
|
68
|
+
assert_called Book, :read
|
69
|
+
```
|
70
|
+
|
71
|
+
You can assert the number of invocations with the optional 3rd argument
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
stub Book, :read do
|
75
|
+
Book.read
|
76
|
+
Book.read
|
77
|
+
end
|
78
|
+
assert_called Book, :read, 2
|
79
|
+
```
|
80
|
+
|
81
|
+
You can refute
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
stub Book, :read do
|
85
|
+
person = Person.new
|
86
|
+
person.say
|
87
|
+
end
|
88
|
+
refute_called Book, :read
|
89
|
+
```
|
90
|
+
|
91
|
+
And you can refute the number of invocations:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
stub Book, :read do
|
95
|
+
Book.read
|
96
|
+
end
|
97
|
+
refute_called Book, :read, 2
|
98
|
+
```
|
99
|
+
|
100
|
+
### Spying on instances
|
101
|
+
|
102
|
+
If you don't have access to the instance of the object in your test you
|
103
|
+
can spy on the instance of a class with `assert_instance_called` and
|
104
|
+
`refute_instance_called`
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
instance_stub Person, :say, "hello" do
|
108
|
+
person = Person.new
|
109
|
+
person.say
|
110
|
+
end
|
111
|
+
|
112
|
+
assert_instance_called Person, :say
|
113
|
+
```
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
instance_stub Person, :say, "hello" do
|
117
|
+
Book.read
|
118
|
+
end
|
119
|
+
|
120
|
+
refute_instance_called Person, :say
|
121
|
+
```
|
122
|
+
|
123
|
+
## Authors ##
|
124
|
+
|
125
|
+
* [Brian Cardarella](http://twitter.com/bcardarella)
|
126
|
+
|
127
|
+
[We are very thankful for the many contributors](https://github.com/dockyard/minitest-moar/graphs/contributors)
|
128
|
+
|
129
|
+
## Versioning ##
|
130
|
+
|
131
|
+
This gem follows [Semantic Versioning](http://semver.org)
|
132
|
+
|
133
|
+
## Want to help? ##
|
134
|
+
|
135
|
+
Please do! We are always looking to improve this gem. Please see our
|
136
|
+
[Contribution Guidelines](https://github.com/dockyard/minitest-moar/blob/master/CONTRIBUTING.md)
|
137
|
+
on how to properly submit issues and pull requests.
|
138
|
+
|
139
|
+
## Legal ##
|
140
|
+
|
141
|
+
[DockYard](http://dockyard.com), Inc. © 2014
|
142
|
+
|
143
|
+
[@dockyard](http://twitter.com/dockyard)
|
144
|
+
|
145
|
+
[Licensed under the MIT license](http://www.opensource.org/licenses/mit-license.php)
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
|
+
|
5
|
+
require 'rake/testtask'
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |t|
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.libs << 'test'
|
10
|
+
t.pattern = 'test/**/*_test.rb'
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
task default: :test
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "minitest/moar/version"
|
2
|
+
|
3
|
+
module Minitest
|
4
|
+
module Moar
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'minitest/moar/stubbing'
|
9
|
+
require 'minitest/moar/assertions'
|
10
|
+
|
11
|
+
Minitest::Test.send(:include, Minitest::Moar::Stubbing)
|
12
|
+
Minitest::Test.send(:include, Minitest::Moar::Assertions)
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Minitest::Moar::Assertions
|
2
|
+
def assert_called object, method, count = nil
|
3
|
+
invoked = get_invocation_count(object, method)
|
4
|
+
|
5
|
+
if count.nil?
|
6
|
+
assert invoked > 0, "#{object} expected to call #{method} but did not."
|
7
|
+
else
|
8
|
+
assert_equal count, invoked, "#{object} expected to call #{method} #{count} times but it was only called #{invoked} times."
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def assert_instance_called object, method, count = nil
|
13
|
+
assert_called "Instance of #{object}", method, count
|
14
|
+
end
|
15
|
+
|
16
|
+
def refute_called object, method, count = nil
|
17
|
+
invoked = get_invocation_count(object, method)
|
18
|
+
|
19
|
+
if count.nil?
|
20
|
+
refute invoked > 0, "#{object} expected to not call #{method} but did."
|
21
|
+
else
|
22
|
+
refute_equal count, invoked, "#{object} expected to not call #{method} #{count} times but it did."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def refute_instance_called object, method, count = nil
|
27
|
+
refute_called "Instance of #{object}", method, count
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def get_invocation_count(object, method)
|
33
|
+
if @invocations
|
34
|
+
@invocations[object][method]
|
35
|
+
else
|
36
|
+
0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Minitest::Moar::Stubbing
|
2
|
+
def stub klass, name, val_or_callable = nil, *block_args, &block
|
3
|
+
@invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
|
4
|
+
|
5
|
+
new_name = "__minitest_stub__#{name}"
|
6
|
+
|
7
|
+
metaclass = class << klass; self; end
|
8
|
+
instance_stub metaclass, name, val_or_callable, block_args, &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def instance_stub klass, name, val_or_callable = nil, *block_args, &block
|
12
|
+
@invocations ||= Hash.new { |h, k| h[k] = Hash.new { |_h, _k| _h[_k] = 0 } }
|
13
|
+
|
14
|
+
invoker = Proc.new { |klass, name| @invocations[klass][name] += 1 }
|
15
|
+
|
16
|
+
new_name = "__minitest_stub__#{name}"
|
17
|
+
|
18
|
+
if respond_to? name and not methods.map(&:to_s).include? name.to_s then
|
19
|
+
klass.send :define_method, name do |*args|
|
20
|
+
super(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
klass.send :alias_method, new_name, name
|
25
|
+
|
26
|
+
klass.send :define_method, name do |*args, &blk|
|
27
|
+
if self.is_a?(Class)
|
28
|
+
invoker.call(self, name)
|
29
|
+
else
|
30
|
+
invoker.call("Instance of #{self.class}", name)
|
31
|
+
end
|
32
|
+
|
33
|
+
ret = if val_or_callable.respond_to? :call then
|
34
|
+
val_or_callable.call(*args)
|
35
|
+
else
|
36
|
+
val_or_callable
|
37
|
+
end
|
38
|
+
|
39
|
+
blk.call(*block_args) if blk
|
40
|
+
|
41
|
+
ret
|
42
|
+
end
|
43
|
+
|
44
|
+
yield klass
|
45
|
+
ensure
|
46
|
+
klass.send :undef_method, name
|
47
|
+
klass.send :alias_method, name, new_name
|
48
|
+
klass.send :undef_method, new_name
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
Object.send(:remove_method, :stub)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minitest/moar/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "minitest-moar"
|
8
|
+
spec.version = Minitest::Moar::VERSION
|
9
|
+
spec.authors = ["Brian Cardarella"]
|
10
|
+
spec.email = ["bcardarella@gmail.com"]
|
11
|
+
spec.summary = %q{Moar Minitest Please!}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/dockyard/minitest-moar"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/test/spy_test.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SpyTest < Minitest::Test
|
4
|
+
def test_spy_works
|
5
|
+
person = Person.new
|
6
|
+
|
7
|
+
stub Book, :read do
|
8
|
+
person.read_book(Book)
|
9
|
+
end
|
10
|
+
|
11
|
+
assert_called Book, :read
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_spy_call_count
|
15
|
+
person = Person.new
|
16
|
+
|
17
|
+
stub Book, :read do
|
18
|
+
person.read_book(Book)
|
19
|
+
person.read_book(Book)
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_called Book, :read, 2
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_refute_spy
|
26
|
+
refute_called Book, :read
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_refute_spy_call_count
|
30
|
+
person = Person.new
|
31
|
+
|
32
|
+
stub Book, :read do
|
33
|
+
person.read_book(Book)
|
34
|
+
person.read_book(Book)
|
35
|
+
end
|
36
|
+
|
37
|
+
refute_called Book, :read, 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_instance_spy_works
|
41
|
+
person = Person.new
|
42
|
+
|
43
|
+
instance_stub Book, :read do
|
44
|
+
person.read_book(Book.new)
|
45
|
+
end
|
46
|
+
|
47
|
+
assert_instance_called Book, :read
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_instance_spy_call_count
|
51
|
+
person = Person.new
|
52
|
+
|
53
|
+
instance_stub Book, :read do
|
54
|
+
person.read_book(Book.new)
|
55
|
+
person.read_book(Book.new)
|
56
|
+
end
|
57
|
+
|
58
|
+
assert_instance_called Book, :read, 2
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_refute_instance_spy
|
62
|
+
refute_called Book, :read
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_refute_instance_spy_call_count
|
66
|
+
person = Person.new
|
67
|
+
|
68
|
+
instance_stub Book, :read do
|
69
|
+
person.read_book(Book.new)
|
70
|
+
person.read_book(Book.new)
|
71
|
+
end
|
72
|
+
|
73
|
+
refute_instance_called Book, :read, 1
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StubbingTest < Minitest::Test
|
4
|
+
def test_no_object_stub
|
5
|
+
refute Object.respond_to?(:stub)
|
6
|
+
refute Object.new.respond_to?(:stub)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_stub_method_exists
|
10
|
+
assert self.respond_to?(:stub)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_instance_stub_method_exists
|
14
|
+
assert self.respond_to?(:instance_stub)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_stub_method_works
|
18
|
+
person = Person.new
|
19
|
+
|
20
|
+
@called = 0
|
21
|
+
caller = Proc.new { @called += 1 }
|
22
|
+
|
23
|
+
stub Book, :read, caller do
|
24
|
+
person.read_book(Book)
|
25
|
+
end
|
26
|
+
|
27
|
+
assert_equal 1, @called
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_instance_stub_method_works
|
31
|
+
person = Person.new
|
32
|
+
|
33
|
+
@called = 0
|
34
|
+
caller = Proc.new { @called += 1 }
|
35
|
+
|
36
|
+
instance_stub Book, :read, caller do
|
37
|
+
person.read_book(Book.new)
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal 1, @called
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minitest-moar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Cardarella
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-20 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: Moar Minitest Please!
|
42
|
+
email:
|
43
|
+
- bcardarella@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/minitest/moar.rb
|
55
|
+
- lib/minitest/moar/assertions.rb
|
56
|
+
- lib/minitest/moar/stubbing.rb
|
57
|
+
- lib/minitest/moar/version.rb
|
58
|
+
- minitest-moar.gemspec
|
59
|
+
- test/spy_test.rb
|
60
|
+
- test/stubbing_test.rb
|
61
|
+
- test/test_helper.rb
|
62
|
+
homepage: https://github.com/dockyard/minitest-moar
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.2.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Moar Minitest Please!
|
86
|
+
test_files:
|
87
|
+
- test/spy_test.rb
|
88
|
+
- test/stubbing_test.rb
|
89
|
+
- test/test_helper.rb
|