invoke_matcher 0.1.0 → 0.2.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/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/lib/invoke_matcher/version.rb +1 -1
- data/lib/invoke_matcher.rb +43 -50
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 975e4d078755b24aaf9874972f3f394db04eab312a53ecc4c251eb4025248763
|
4
|
+
data.tar.gz: 8b72b943934a89bdd02869da51accfd88abf539f5f4c96cbde2d826774811a37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de4a6738c8c6d6df78236286b4599994c6a34bc0969ba10e1e1a2d3d5bf56119c2c072ffd4f50e99ce190b9414a25b8aebe46ba525b9ac30c5b3fb3d5903e06e
|
7
|
+
data.tar.gz: d4a3577643c9fa90fe1af113b40cf0370eb24aa44b9887b6f177eeae44e13e699eb9cb7c7e2b4ea4307d84005f3c7545b1c7f8763c8214188ecf7d72ebe1dc45
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Tyler Rhodes
|
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,58 @@
|
|
1
|
+
# InvokeMatcher
|
2
|
+
|
3
|
+
This is adapted from: https://github.com/rspec/rspec-expectations/issues/934
|
4
|
+
|
5
|
+
It allows us to write expectations in a more declarative way, by checking if a
|
6
|
+
method was called on a class or instance.
|
7
|
+
|
8
|
+
Old way:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
allow(foo).to receive(:method).and_return('value')
|
12
|
+
subject
|
13
|
+
expect(foo).to have_received(:method)
|
14
|
+
```
|
15
|
+
|
16
|
+
Or:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
expect(foo).to receive(:method).and_return('value')
|
20
|
+
subject
|
21
|
+
```
|
22
|
+
|
23
|
+
New way:
|
24
|
+
```ruby
|
25
|
+
expect { subject }.to invoke(:method).on(foo).with('bar').and_return('value')
|
26
|
+
```
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
Add this to your test gems:
|
30
|
+
```ruby
|
31
|
+
group :test do
|
32
|
+
gem 'invoke_matcher'
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Make sure to require it in your `spec_helper.rb` or `rails_helper.rb`:
|
37
|
+
```ruby
|
38
|
+
require 'invoke_matcher'
|
39
|
+
|
40
|
+
RSpec.configure do |config|
|
41
|
+
config.include InvokeMatcher
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
```ruby
|
47
|
+
expect { foo }.to invoke(:method).on(Class).and_call_original
|
48
|
+
|
49
|
+
expect { foo }.to change{ bar }.and not_invoke(:method).on(Class)
|
50
|
+
|
51
|
+
expect { foo }.to invoke(:method).on(Class).at_least(3).times
|
52
|
+
|
53
|
+
expect { foo }.to invoke(:method).and_expect_return('bar')
|
54
|
+
```
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/invoke_matcher.rb
CHANGED
@@ -11,63 +11,30 @@ module InvokeMatcher
|
|
11
11
|
|
12
12
|
def_delegators :have_received_matcher, :failure_message, :failure_message_when_negated
|
13
13
|
|
14
|
-
attr_reader :have_received_matcher, :expected_recipient
|
14
|
+
attr_reader :have_received_matcher, :expected_recipient, :expected_method
|
15
15
|
|
16
16
|
def initialize(expected_method)
|
17
17
|
@expected_method = expected_method
|
18
|
-
@have_received_matcher = RSpec::Mocks::Matchers::HaveReceived.new(
|
18
|
+
@have_received_matcher = RSpec::Mocks::Matchers::HaveReceived.new(expected_method)
|
19
19
|
end
|
20
20
|
|
21
21
|
def description
|
22
|
-
"invoke #{
|
23
|
-
end
|
24
|
-
|
25
|
-
def and_call_original
|
26
|
-
@call_original = true
|
27
|
-
self
|
28
|
-
end
|
29
|
-
|
30
|
-
def method_missing(name, ...)
|
31
|
-
ensure_recipient_is_defined!
|
32
|
-
return super unless respond_to_missing?(name)
|
33
|
-
|
34
|
-
@have_received_matcher = have_received_matcher.public_send(name, ...)
|
35
|
-
self
|
36
|
-
end
|
37
|
-
|
38
|
-
def respond_to_missing?(name, include_private = false)
|
39
|
-
@have_received_matcher.respond_to?(name, include_private)
|
22
|
+
"invoke #{expected_method} on #{expected_recipient.inspect}"
|
40
23
|
end
|
41
24
|
|
42
25
|
def matches?(event_proc)
|
43
26
|
ensure_recipient_is_defined!
|
44
27
|
set_method_expectation
|
45
|
-
|
28
|
+
setup_return_value_check if defined?(@expected_return)
|
29
|
+
|
46
30
|
event_proc.call
|
47
|
-
|
48
|
-
have_received_matcher.matches?(expected_recipient) && matches_result?
|
49
|
-
end
|
50
|
-
|
51
|
-
def set_method_expectation
|
52
|
-
return if testing_double?
|
53
|
-
|
54
|
-
@actual_result = expected_recipient.send(@expected_method)
|
55
|
-
expectation = allow(expected_recipient).to receive(@expected_method)
|
56
|
-
expectation.and_call_original if @call_original
|
57
|
-
end
|
58
|
-
|
59
|
-
def testing_double?
|
60
|
-
@expected_recipient.is_a?(RSpec::Mocks::Double) ||
|
61
|
-
@expected_recipient.is_a?(RSpec::Mocks::InstanceVerifyingDouble) ||
|
62
|
-
@expected_recipient.is_a?(RSpec::Mocks::ObjectVerifyingDouble)
|
31
|
+
have_received_matcher.matches?(expected_recipient)
|
63
32
|
end
|
64
33
|
|
65
34
|
def does_not_match?(event_proc)
|
66
35
|
ensure_recipient_is_defined!
|
67
36
|
set_method_expectation
|
68
|
-
|
69
37
|
event_proc.call
|
70
|
-
|
71
38
|
have_received_matcher.does_not_match?(expected_recipient)
|
72
39
|
end
|
73
40
|
|
@@ -81,34 +48,60 @@ module InvokeMatcher
|
|
81
48
|
self
|
82
49
|
end
|
83
50
|
|
84
|
-
def
|
85
|
-
true
|
51
|
+
def and_call_original
|
52
|
+
@call_original = true
|
53
|
+
self
|
86
54
|
end
|
87
55
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
values_match?(@expected_return_value, @actual_result)
|
56
|
+
def and_expect_return(expected_value)
|
57
|
+
@expected_return = expected_value
|
58
|
+
self
|
92
59
|
end
|
93
60
|
|
94
|
-
def
|
95
|
-
|
61
|
+
def method_missing(name, ...)
|
62
|
+
ensure_recipient_is_defined!
|
63
|
+
return super unless have_received_matcher.respond_to?(name)
|
64
|
+
|
65
|
+
@have_received_matcher = have_received_matcher.public_send(name, ...)
|
96
66
|
self
|
97
67
|
end
|
98
68
|
|
69
|
+
def respond_to_missing?(name, include_private = false)
|
70
|
+
have_received_matcher.respond_to?(name, include_private)
|
71
|
+
end
|
72
|
+
|
73
|
+
def supports_block_expectations?
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
99
77
|
private
|
100
78
|
|
101
79
|
def ensure_recipient_is_defined!
|
102
80
|
raise ArgumentError, "missing `on`" unless defined?(expected_recipient)
|
103
81
|
end
|
104
82
|
|
105
|
-
def
|
106
|
-
|
83
|
+
def set_method_expectation
|
84
|
+
allow(expected_recipient).to receive(expected_method)
|
85
|
+
end
|
86
|
+
|
87
|
+
def setup_return_value_check
|
88
|
+
allow(expected_recipient).to receive(expected_method) do |*args, **kwargs|
|
89
|
+
actual_value = expected_recipient.method(expected_method).super_method.call(*args, **kwargs)
|
90
|
+
verify_return_value(actual_value)
|
91
|
+
actual_value
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def verify_return_value(actual_value)
|
96
|
+
return if actual_value == @expected_return
|
97
|
+
|
98
|
+
raise RSpec::Expectations::ExpectationNotMetError,
|
99
|
+
"expected #{expected_method} to return #{@expected_return.inspect} but got #{actual_value.inspect}"
|
107
100
|
end
|
108
101
|
end
|
109
102
|
|
110
103
|
def invoke(expected_method)
|
111
|
-
|
104
|
+
Matcher.new(expected_method)
|
112
105
|
end
|
113
106
|
end
|
114
107
|
|
metadata
CHANGED
@@ -1,22 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invoke_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Rhodes
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-
|
11
|
+
date: 2025-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: A Ruby gem that provides RSpec matchers for verifying method invocations
|
14
|
+
with specific arguments
|
14
15
|
email:
|
15
16
|
- rhodetyl000@gmail.com
|
16
17
|
executables: []
|
17
18
|
extensions: []
|
18
19
|
extra_rdoc_files: []
|
19
20
|
files:
|
21
|
+
- LICENSE.txt
|
22
|
+
- README.md
|
20
23
|
- lib/invoke_matcher.rb
|
21
24
|
- lib/invoke_matcher/version.rb
|
22
25
|
homepage: https://github.com/tylerCaineRhodes/invoke_matcher
|
@@ -26,7 +29,7 @@ metadata:
|
|
26
29
|
homepage_uri: https://github.com/tylerCaineRhodes/invoke_matcher
|
27
30
|
source_code_uri: https://github.com/tylerCaineRhodes/invoke_matcher
|
28
31
|
rubygems_mfa_required: 'true'
|
29
|
-
post_install_message:
|
32
|
+
post_install_message:
|
30
33
|
rdoc_options: []
|
31
34
|
require_paths:
|
32
35
|
- lib
|
@@ -41,8 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
44
|
- !ruby/object:Gem::Version
|
42
45
|
version: '0'
|
43
46
|
requirements: []
|
44
|
-
rubygems_version: 3.
|
45
|
-
signing_key:
|
47
|
+
rubygems_version: 3.4.10
|
48
|
+
signing_key:
|
46
49
|
specification_version: 4
|
47
50
|
summary: RSpec matcher for testing method invocations
|
48
51
|
test_files: []
|