minitest-mock_expectations 0.9.0 → 1.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile.lock +2 -2
- data/README.md +32 -24
- data/lib/minitest/mock_expectations/assertions.rb +29 -24
- data/lib/minitest/mock_expectations/version.rb +1 -1
- data/minitest-mock_expectations.gemspec +1 -1
- data/test/minitest/mock_expectations/assertions_test.rb +53 -20
- metadata +3 -4
- data/.gitignore +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01ebd3a13e010b674980be321ca63719fa38bf64d7c048d821cc4cf1347754af
|
4
|
+
data.tar.gz: 8519365b930a963f82593a4b3d59bff209889f69d1e2e53a59c4d4d01331ccee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d823714a5f198e096f308200bd902d13fe5536df4595de8e6b68ff276518b680a1bf2bf81853645f268a55398d79e780f1c1a38b6b31a6fdc5c9b0a16865c8e
|
7
|
+
data.tar.gz: 3ac1d6d54e1efb6442c36a2a76773f3b5e70196ab98b380501ef8312daf94644b28c4aff5d6682c4c31d1733fc7933f2c6de229fcf7c212a2e15becf9979e1ac
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# minitest-mock_expectations
|
2
2
|
|
3
|
-
Provides method call assertions
|
3
|
+
Provides method call assertions for minitest
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem
|
10
|
+
gem "minitest-mock_expectations"
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -58,23 +58,23 @@ def setup
|
|
58
58
|
body: "https://bogdanvlviv.com/posts/ruby/rails/what-is-new-in-rails-6_0.html",
|
59
59
|
comments: [
|
60
60
|
"Looking really good.",
|
61
|
-
"I
|
61
|
+
"I really like this post."
|
62
62
|
]
|
63
63
|
)
|
64
64
|
end
|
65
65
|
```
|
66
66
|
|
67
|
-
### assert_called
|
67
|
+
### assert_called(object, method_name, message = nil, times: 1, returns: nil)
|
68
68
|
|
69
|
-
Asserts that the method will be called
|
69
|
+
Asserts that the method will be called on the `object` in the block
|
70
70
|
|
71
71
|
```ruby
|
72
|
-
assert_called
|
72
|
+
assert_called(@post, :title) do
|
73
73
|
@post.title
|
74
74
|
end
|
75
75
|
```
|
76
76
|
|
77
|
-
In order to assert that the method will be called multiple times on the object in the block set `:times` option:
|
77
|
+
In order to assert that the method will be called multiple times on the `object` in the block set `:times` option:
|
78
78
|
|
79
79
|
```ruby
|
80
80
|
assert_called(@post, :title, times: 2) do
|
@@ -93,19 +93,23 @@ end
|
|
93
93
|
assert_equal "What is new in Rails 6.0", @object.title
|
94
94
|
```
|
95
95
|
|
96
|
-
###
|
96
|
+
### refute_called(object, method_name, message = nil, &block)
|
97
97
|
|
98
|
-
Asserts that the method will not be called on the object in the block
|
98
|
+
Asserts that the method will not be called on the `object` in the block
|
99
99
|
|
100
100
|
```ruby
|
101
|
-
|
101
|
+
refute_called(@post, :title) do
|
102
102
|
@post.body
|
103
103
|
end
|
104
104
|
```
|
105
105
|
|
106
|
-
###
|
106
|
+
### assert_not_called
|
107
|
+
|
108
|
+
Alias for `refute_called`.
|
109
|
+
|
110
|
+
### assert_called_with(object, method_name, arguments, returns: nil)
|
107
111
|
|
108
|
-
Asserts that the method will be called with the arguments
|
112
|
+
Asserts that the method will be called with the `arguments` on the `object` in the block
|
109
113
|
|
110
114
|
```ruby
|
111
115
|
assert_called_with(@post, :add_comment, ["Thanks for sharing this."]) do
|
@@ -123,7 +127,7 @@ end
|
|
123
127
|
assert_equal "Thank you!", @post.add_comment("Thanks for sharing this.")
|
124
128
|
```
|
125
129
|
|
126
|
-
You can also assert that the method will be called with different arguments
|
130
|
+
You can also assert that the method will be called with different `arguments` on the `object` in the block:
|
127
131
|
|
128
132
|
```ruby
|
129
133
|
assert_called_with(@post, :add_comment, [["Thanks for sharing this."], ["Thanks!"]]) do
|
@@ -132,17 +136,17 @@ assert_called_with(@post, :add_comment, [["Thanks for sharing this."], ["Thanks!
|
|
132
136
|
end
|
133
137
|
```
|
134
138
|
|
135
|
-
### assert_called_on_instance_of
|
139
|
+
### assert_called_on_instance_of(klass, method_name, message = nil, times: 1, returns: nil)
|
136
140
|
|
137
|
-
Asserts that the method will be called
|
141
|
+
Asserts that the method will be called on an instance of the `klass` in the block
|
138
142
|
|
139
143
|
```ruby
|
140
|
-
assert_called_on_instance_of
|
144
|
+
assert_called_on_instance_of(Post, :title) do
|
141
145
|
@post.title
|
142
146
|
end
|
143
147
|
```
|
144
148
|
|
145
|
-
In order to assert that the method will be called multiple times on an instance of the klass in the block set `:times` option:
|
149
|
+
In order to assert that the method will be called multiple times on an instance of the `klass` in the block set `:times` option:
|
146
150
|
|
147
151
|
```ruby
|
148
152
|
assert_called_on_instance_of(Post, :title, times: 2) do
|
@@ -161,7 +165,7 @@ end
|
|
161
165
|
assert_equal "What is new in Rails 6.0", @post.title
|
162
166
|
```
|
163
167
|
|
164
|
-
Use nesting of the blocks in order assert that the several methods will be called on an instance of the klass in the block:
|
168
|
+
Use nesting of the blocks in order assert that the several methods will be called on an instance of the `klass` in the block:
|
165
169
|
|
166
170
|
```ruby
|
167
171
|
assert_called_on_instance_of(Post, :title, times: 3) do
|
@@ -175,25 +179,29 @@ assert_called_on_instance_of(Post, :title, times: 3) do
|
|
175
179
|
end
|
176
180
|
```
|
177
181
|
|
178
|
-
###
|
182
|
+
### refute_called_on_instance_of(klass, method_name, message = nil, &block)
|
179
183
|
|
180
|
-
Asserts that the method will not be called
|
184
|
+
Asserts that the method will not be called on an instance of the `klass` in the block
|
181
185
|
|
182
186
|
```ruby
|
183
|
-
|
187
|
+
refute_called(@post, :title) do
|
184
188
|
@post.body
|
185
189
|
end
|
186
190
|
```
|
187
191
|
|
188
|
-
Use nesting of the blocks in order assert that the several methods will not be called on an instance of the klass in the block:
|
192
|
+
Use nesting of the blocks in order assert that the several methods will not be called on an instance of the `klass` in the block:
|
189
193
|
|
190
194
|
```ruby
|
191
|
-
|
192
|
-
|
195
|
+
refute_called_on_instance_of(Post, :title) do
|
196
|
+
refute_called_on_instance_of(Post, :body) do
|
193
197
|
end
|
194
198
|
end
|
195
199
|
```
|
196
200
|
|
201
|
+
### assert_not_called_on_instance_of
|
202
|
+
|
203
|
+
Alias for `refute_called_on_instance_of`.
|
204
|
+
|
197
205
|
## Contributing
|
198
206
|
|
199
207
|
Bug reports and pull requests are welcome on GitHub at https://github.com/bogdanvlviv/minitest-mock_expectations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.
|
@@ -3,7 +3,7 @@ require "minitest/assertions"
|
|
3
3
|
require "minitest/mock"
|
4
4
|
|
5
5
|
module Minitest
|
6
|
-
# Provides method call assertions
|
6
|
+
# Provides method call assertions for minitest
|
7
7
|
#
|
8
8
|
# Imagine we have model +Post+:
|
9
9
|
#
|
@@ -32,18 +32,18 @@ module Minitest
|
|
32
32
|
# body: "https://bogdanvlviv.com/posts/ruby/rails/what-is-new-in-rails-6_0.html",
|
33
33
|
# comments: [
|
34
34
|
# "Looking really good.",
|
35
|
-
# "I
|
35
|
+
# "I really like this post."
|
36
36
|
# ]
|
37
37
|
# )
|
38
38
|
# end
|
39
39
|
module Assertions
|
40
|
-
# Asserts that the method will be called
|
40
|
+
# Asserts that the method will be called on the +object+ in the block
|
41
41
|
#
|
42
|
-
# assert_called
|
42
|
+
# assert_called(@post, :title) do
|
43
43
|
# @post.title
|
44
44
|
# end
|
45
45
|
#
|
46
|
-
# In order to assert that the method will be called multiple times on the object in the block set +:times+ option:
|
46
|
+
# In order to assert that the method will be called multiple times on the +object+ in the block set +:times+ option:
|
47
47
|
#
|
48
48
|
# assert_called(@post, :title, times: 2) do
|
49
49
|
# @post.title
|
@@ -67,16 +67,18 @@ module Minitest
|
|
67
67
|
assert_equal times, times_called, error
|
68
68
|
end
|
69
69
|
|
70
|
-
# Asserts that the method will not be called on the object in the block
|
70
|
+
# Asserts that the method will not be called on the +object+ in the block
|
71
71
|
#
|
72
|
-
#
|
72
|
+
# refute_called(@post, :title) do
|
73
73
|
# @post.body
|
74
74
|
# end
|
75
|
-
def
|
75
|
+
def refute_called(object, method_name, message = nil, &block)
|
76
76
|
assert_called(object, method_name, message, times: 0, &block)
|
77
77
|
end
|
78
78
|
|
79
|
-
|
79
|
+
alias assert_not_called refute_called
|
80
|
+
|
81
|
+
# Asserts that the method will be called with the +arguments+ on the +object+ in the block
|
80
82
|
#
|
81
83
|
# assert_called_with(@post, :add_comment, ["Thanks for sharing this."]) do
|
82
84
|
# @post.add_comment("Thanks for sharing this.")
|
@@ -90,19 +92,19 @@ module Minitest
|
|
90
92
|
#
|
91
93
|
# assert_equal "Thank you!", @post.add_comment("Thanks for sharing this.")
|
92
94
|
#
|
93
|
-
# You can also assert that the method will be called with different arguments
|
95
|
+
# You can also assert that the method will be called with different +arguments+ on the +object+ in the block:
|
94
96
|
#
|
95
97
|
# assert_called_with(@post, :add_comment, [["Thanks for sharing this."], ["Thanks!"]]) do
|
96
98
|
# @post.add_comment("Thanks for sharing this.")
|
97
99
|
# @post.add_comment("Thanks!")
|
98
100
|
# end
|
99
|
-
def assert_called_with(object, method_name,
|
101
|
+
def assert_called_with(object, method_name, arguments, returns: nil)
|
100
102
|
mock = Minitest::Mock.new
|
101
103
|
|
102
|
-
if
|
103
|
-
|
104
|
+
if arguments.all? { |argument| argument.is_a?(Array) }
|
105
|
+
arguments.each { |argument| mock.expect(:call, returns, argument) }
|
104
106
|
else
|
105
|
-
mock.expect(:call, returns,
|
107
|
+
mock.expect(:call, returns, arguments)
|
106
108
|
end
|
107
109
|
|
108
110
|
object.stub(method_name, mock) { yield }
|
@@ -110,13 +112,13 @@ module Minitest
|
|
110
112
|
mock.verify
|
111
113
|
end
|
112
114
|
|
113
|
-
# Asserts that the method will be called
|
115
|
+
# Asserts that the method will be called on an instance of the +klass+ in the block
|
114
116
|
#
|
115
|
-
# assert_called_on_instance_of
|
117
|
+
# assert_called_on_instance_of(Post, :title) do
|
116
118
|
# @post.title
|
117
119
|
# end
|
118
120
|
#
|
119
|
-
# In order to assert that the method will be called multiple times on an instance of the klass in the block set +:times+ option:
|
121
|
+
# In order to assert that the method will be called multiple times on an instance of the +klass+ in the block set +:times+ option:
|
120
122
|
#
|
121
123
|
# assert_called_on_instance_of(Post, :title, times: 2) do
|
122
124
|
# @post.title
|
@@ -131,7 +133,7 @@ module Minitest
|
|
131
133
|
#
|
132
134
|
# assert_equal "What is new in Rails 6.0", @post.title
|
133
135
|
#
|
134
|
-
# Use nesting of the blocks in order assert that the several methods will be called on an instance of the klass in the block:
|
136
|
+
# Use nesting of the blocks in order assert that the several methods will be called on an instance of the +klass+ in the block:
|
135
137
|
#
|
136
138
|
# assert_called_on_instance_of(Post, :title, times: 3) do
|
137
139
|
# assert_called_on_instance_of(Post, :body, times: 2) do
|
@@ -166,20 +168,23 @@ module Minitest
|
|
166
168
|
klass.send(:undef_method, "stubbed_#{method_name}")
|
167
169
|
end
|
168
170
|
|
169
|
-
# Asserts that the method will not be called
|
171
|
+
# Asserts that the method will not be called on an instance of the +klass+ in the block
|
170
172
|
#
|
171
|
-
#
|
173
|
+
# refute_called_on_instance_of(Post, :title) do
|
172
174
|
# @post.body
|
173
175
|
# end
|
174
176
|
#
|
175
|
-
# Use nesting of the blocks in order assert that the several methods will not be called on an instance of the klass in the block:
|
177
|
+
# Use nesting of the blocks in order assert that the several methods will not be called on an instance of the +klass+ in the block:
|
176
178
|
#
|
177
|
-
#
|
178
|
-
#
|
179
|
+
# refute_called_on_instance_of(Post, :title) do
|
180
|
+
# refute_called_on_instance_of(Post, :body) do
|
181
|
+
# @post.add_comment("Thanks for sharing this.")
|
179
182
|
# end
|
180
183
|
# end
|
181
|
-
def
|
184
|
+
def refute_called_on_instance_of(klass, method_name, message = nil, &block)
|
182
185
|
assert_called_on_instance_of(klass, method_name, message, times: 0, &block)
|
183
186
|
end
|
187
|
+
|
188
|
+
alias assert_not_called_on_instance_of refute_called_on_instance_of
|
184
189
|
end
|
185
190
|
end
|
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["bogdanvlviv"]
|
9
9
|
spec.email = ["bogdanvlviv@gmail.com"]
|
10
10
|
|
11
|
-
spec.summary = "Provides method call assertions"
|
11
|
+
spec.summary = "Provides method call assertions for minitest"
|
12
12
|
spec.description = ""
|
13
13
|
spec.homepage = "https://github.com/bogdanvlviv/minitest-mock_expectations"
|
14
14
|
spec.license = "MIT"
|
@@ -24,13 +24,13 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
24
24
|
body: "https://bogdanvlviv.com/posts/ruby/rails/what-is-new-in-rails-6_0.html",
|
25
25
|
comments: [
|
26
26
|
"Looking really good.",
|
27
|
-
"I
|
27
|
+
"I really like this post."
|
28
28
|
]
|
29
29
|
)
|
30
30
|
end
|
31
31
|
|
32
32
|
def test_assert_called_with_defaults_to_expect_once
|
33
|
-
assert_called
|
33
|
+
assert_called(@post, :title) do
|
34
34
|
@post.title
|
35
35
|
end
|
36
36
|
end
|
@@ -59,35 +59,51 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
59
59
|
def test_assert_called_failure
|
60
60
|
error = assert_raises(Minitest::Assertion) do
|
61
61
|
assert_called(@post, :title) do
|
62
|
+
@post.body
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
66
|
assert_equal "Expected title to be called 1 times, but was called 0 times.\nExpected: 1\n Actual: 0", error.message
|
66
67
|
end
|
67
68
|
|
68
|
-
def
|
69
|
-
|
69
|
+
def test_assert_called_with_message
|
70
|
+
error = assert_raises(Minitest::Assertion) do
|
71
|
+
assert_called(@post, :title, "Message") do
|
72
|
+
@post.body
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_equal "Message.\nExpected title to be called 1 times, but was called 0 times.\nExpected: 1\n Actual: 0", error.message
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_refute_called
|
80
|
+
refute_called(@post, :title) do
|
70
81
|
@post.body
|
71
82
|
end
|
72
83
|
end
|
73
84
|
|
74
|
-
def
|
85
|
+
def test_refute_called_failure
|
75
86
|
error = assert_raises(Minitest::Assertion) do
|
76
|
-
|
77
|
-
@post.
|
87
|
+
refute_called(@post, :add_comment) do
|
88
|
+
@post.add_comment("Thanks for sharing this.")
|
78
89
|
end
|
79
90
|
end
|
80
91
|
|
81
|
-
assert_equal "Expected
|
92
|
+
assert_equal "Expected add_comment to be called 0 times, but was called 1 times.\nExpected: 0\n Actual: 1", error.message
|
82
93
|
end
|
83
94
|
|
84
|
-
def
|
95
|
+
def test_refute_called_with_message
|
85
96
|
error = assert_raises(Minitest::Assertion) do
|
86
|
-
|
97
|
+
refute_called(@post, :title, "Message") do
|
98
|
+
@post.title
|
87
99
|
end
|
88
100
|
end
|
89
101
|
|
90
|
-
assert_equal "Message.\nExpected title to be called
|
102
|
+
assert_equal "Message.\nExpected title to be called 0 times, but was called 1 times.\nExpected: 0\n Actual: 1", error.message
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_assert_not_called_is_alias_for_refute_called
|
106
|
+
assert method(:assert_not_called).eql?(method(:refute_called))
|
91
107
|
end
|
92
108
|
|
93
109
|
def test_assert_called_with_arguments
|
@@ -120,7 +136,7 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
120
136
|
end
|
121
137
|
|
122
138
|
def test_assert_called_on_instance_of_with_defaults_to_expect_once
|
123
|
-
assert_called_on_instance_of
|
139
|
+
assert_called_on_instance_of(Post, :title) do
|
124
140
|
@post.title
|
125
141
|
end
|
126
142
|
end
|
@@ -149,6 +165,7 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
149
165
|
def test_assert_called_on_instance_of_failure
|
150
166
|
error = assert_raises(Minitest::Assertion) do
|
151
167
|
assert_called_on_instance_of(Post, :title) do
|
168
|
+
@post.body
|
152
169
|
end
|
153
170
|
end
|
154
171
|
|
@@ -158,6 +175,7 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
158
175
|
def test_assert_called_on_instance_of_with_message
|
159
176
|
error = assert_raises(Minitest::Assertion) do
|
160
177
|
assert_called_on_instance_of(Post, :title, "Message") do
|
178
|
+
@post.body
|
161
179
|
end
|
162
180
|
end
|
163
181
|
|
@@ -176,26 +194,41 @@ class Minitest::MockExpectations::AssertionsTest < Minitest::Test
|
|
176
194
|
end
|
177
195
|
end
|
178
196
|
|
179
|
-
def
|
180
|
-
|
197
|
+
def test_refute_called_on_instance_of
|
198
|
+
refute_called_on_instance_of(Post, :title) do
|
181
199
|
@post.body
|
182
200
|
end
|
183
201
|
end
|
184
202
|
|
185
|
-
def
|
203
|
+
def test_refute_called_on_instance_of_failure
|
204
|
+
error = assert_raises(Minitest::Assertion) do
|
205
|
+
refute_called_on_instance_of(Post, :add_comment) do
|
206
|
+
@post.add_comment("Thanks for sharing this.")
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
assert_equal "Expected add_comment to be called 0 times, but was called 1 times.\nExpected: 0\n Actual: 1", error.message
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_refute_called_on_instance_of_with_message
|
186
214
|
error = assert_raises(Minitest::Assertion) do
|
187
|
-
|
215
|
+
refute_called_on_instance_of(Post, :title, "Message") do
|
188
216
|
@post.title
|
189
217
|
end
|
190
218
|
end
|
191
219
|
|
192
|
-
assert_equal "
|
220
|
+
assert_equal "Message.\nExpected title to be called 0 times, but was called 1 times.\nExpected: 0\n Actual: 1", error.message
|
193
221
|
end
|
194
222
|
|
195
|
-
def
|
196
|
-
|
197
|
-
|
223
|
+
def test_refute_called_on_instance_of_nesting
|
224
|
+
refute_called_on_instance_of(Post, :title) do
|
225
|
+
refute_called_on_instance_of(Post, :body) do
|
226
|
+
@post.add_comment("Thanks for sharing this.")
|
198
227
|
end
|
199
228
|
end
|
200
229
|
end
|
230
|
+
|
231
|
+
def test_assert_called_on_instance_of_is_alias_for_refute_called
|
232
|
+
assert method(:assert_not_called_on_instance_of).eql?(method(:refute_called_on_instance_of))
|
233
|
+
end
|
201
234
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minitest-mock_expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bogdanvlviv
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,7 +59,6 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
- ".gitignore"
|
63
62
|
- ".travis.yml"
|
64
63
|
- Gemfile
|
65
64
|
- Gemfile.lock
|
@@ -96,5 +95,5 @@ rubyforge_project:
|
|
96
95
|
rubygems_version: 2.7.6
|
97
96
|
signing_key:
|
98
97
|
specification_version: 4
|
99
|
-
summary: Provides method call assertions
|
98
|
+
summary: Provides method call assertions for minitest
|
100
99
|
test_files: []
|
data/.gitignore
DELETED
File without changes
|