rspec-forward 0.1.3 → 0.1.4
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/Gemfile.lock +1 -1
- data/README.md +102 -11
- data/lib/rspec/forward/forward_methods.rb +15 -2
- data/lib/rspec/forward/forward_to_instance.rb +7 -0
- data/lib/rspec/forward/forward_to_instance_build.rb +7 -0
- data/lib/rspec/forward/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b3205b7050cdf8f0bef33291f5015b5e345757ea3ebfa39338f370e7bdcb071
|
4
|
+
data.tar.gz: 473a350e1c4f61586f7804d23a29c854045a96c5ce7848d0350c80d007132514
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0682f9ce361a22f8b9115d1edee289f09b0c5dd4d87591f361690fc065505db1d38492805291ccad5486a0eb616e6430f5387e6781f98b73a89a6c0aecdbbfb
|
7
|
+
data.tar.gz: 0b14a346a42976da92a7f7534aae05edab58d1b25633ddf3cb34b71f5ccaef6e738e6ff11026f8325d613b8da485858d1a617616f1fe047a8aad13f539ea8a1e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
# RSpec::Forward
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Gem `rspec-forward` adds simple matchers to verify short hand class proxy
|
4
|
+
method forwarding when using Method Object pattern.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
9
8
|
Add this line to your application's Gemfile:
|
10
9
|
|
11
10
|
```ruby
|
12
|
-
gem
|
11
|
+
gem "rspec-forward"
|
13
12
|
```
|
14
13
|
|
15
14
|
And then execute:
|
@@ -20,20 +19,110 @@ Or install it yourself as:
|
|
20
19
|
|
21
20
|
$ gem install rspec-forward
|
22
21
|
|
22
|
+
|
23
|
+
Next, include matchers in your `spec/rails_helper.rb` or `spec/spec_helper.rb` in the
|
24
|
+
`RSpec.configure` block:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
RSpec.configure do |config|
|
28
|
+
# ...
|
29
|
+
config.include RSpec::Forward
|
30
|
+
# ...
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
If you are using [Spring](https://github.com/rails/spring) for your Rails
|
35
|
+
development, make sure the daemon is restarted after the instalation step is
|
36
|
+
complete: `spring stop`.
|
37
|
+
|
23
38
|
## Usage
|
24
39
|
|
25
|
-
|
40
|
+
Assume you have a Method Object with `#call` method defined like this:
|
26
41
|
|
27
|
-
|
42
|
+
```ruby
|
43
|
+
class Add
|
44
|
+
def self.call(...)
|
45
|
+
new(...).call
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(addend1, addend2)
|
49
|
+
@addend1 = addend1
|
50
|
+
@addend2 = addend2
|
51
|
+
end
|
52
|
+
|
53
|
+
def call
|
54
|
+
@addend1 + @addend2
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Add.call(3, 5) # => 8
|
59
|
+
```
|
28
60
|
|
29
|
-
|
61
|
+
Gem `rspec-forward` adds matcher `forward_to_instance` to verify if the
|
62
|
+
call using Class Methods is properly building the instance of the calss and
|
63
|
+
forwarding the method call to expected instance method.
|
30
64
|
|
31
|
-
|
65
|
+
```ruby
|
66
|
+
require "rails_helper"
|
32
67
|
|
33
|
-
|
68
|
+
RSpec.describe Add do
|
69
|
+
describe "#call" do
|
70
|
+
let(:service) { described_class.new(3, 5) }
|
34
71
|
|
35
|
-
|
72
|
+
it "adds numbers" do
|
73
|
+
expect(service.call).to eq 8
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe ".call" do
|
78
|
+
subject { described_class }
|
79
|
+
|
80
|
+
it { should forward_to_instance(:call).with_2_args }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
If you would rather not use the old, deprecated
|
86
|
+
`should` syntax in RSpec, replace the above exaple with the following:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
describe ".call" do
|
90
|
+
let(:object) { described_class }
|
91
|
+
|
92
|
+
it "passes arguments to instance" do
|
93
|
+
expect(object)
|
94
|
+
.to forward_to_instance(:call)
|
95
|
+
.with_2_args
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
Possible calls to the matcher include the following:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
it { should forward_to_instance(:call).with_no_args }
|
104
|
+
it { should forward_to_instance(:call).with_1_arg }
|
105
|
+
it { should forward_to_instance(:call).with_2_args }
|
106
|
+
it { should forward_to_instance(:call).with_3_args }
|
107
|
+
# ...
|
108
|
+
|
109
|
+
it { should forward_to_instance(:call).with_named(:foo, :bar) }
|
110
|
+
it { should forward_to_instance(:call).with_1_arg_and_named(:foo) }
|
111
|
+
it { should forward_to_instance(:call).with_2_args_and_named(:foo) }
|
112
|
+
it { should forward_to_instance(:call).with_3_args_and_named(:foo, :bar) }
|
113
|
+
```
|
114
|
+
|
115
|
+
TODO: Explain `forward_to_instance_build(...)`
|
116
|
+
|
117
|
+
## Development
|
118
|
+
|
119
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run
|
120
|
+
`bundle exec rspec` to run the tests.
|
121
|
+
|
122
|
+
## Contributing
|
36
123
|
|
124
|
+
Bug reports and pull requests are welcome on GitHub at
|
125
|
+
https://github.com/adlugopolski/rspec-forward.
|
37
126
|
|
38
127
|
## License
|
39
128
|
|
@@ -41,4 +130,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
41
130
|
|
42
131
|
## Code of Conduct
|
43
132
|
|
44
|
-
Everyone interacting in the RSpec::Forward project's codebases, issue
|
133
|
+
Everyone interacting in the RSpec::Forward project's codebases, issue
|
134
|
+
trackers, chat rooms and mailing lists is expected to follow the [code of
|
135
|
+
conduct](https://github.com/adlugopolski/rspec-forward/blob/master/CODE_OF_CONDUCT.md).
|
@@ -3,15 +3,28 @@ module ::RSpec
|
|
3
3
|
module ForwardMethods
|
4
4
|
0.upto(10) do |index|
|
5
5
|
define_method format("with_%<count>d_args", count: index) do
|
6
|
+
@kwargs = {}
|
6
7
|
@args = Array.new(index, :arg)
|
7
8
|
self
|
8
9
|
end
|
10
|
+
|
11
|
+
name = format("with_%<count>d_args_and_named", count: index)
|
12
|
+
|
13
|
+
define_method name do |*kwargs|
|
14
|
+
@args = Array.new(index, :arg)
|
15
|
+
@kwargs = Hash[kwargs.map { [_1, _1] }]
|
16
|
+
self
|
17
|
+
end
|
9
18
|
end
|
10
19
|
|
11
20
|
def with_1_arg
|
12
21
|
with_1_args
|
13
22
|
end
|
14
23
|
|
24
|
+
def with_1_arg_and_named(*args, **kwargs)
|
25
|
+
with_1_args_and_named(*args, **kwargs)
|
26
|
+
end
|
27
|
+
|
15
28
|
def with_no_args
|
16
29
|
with_0_args
|
17
30
|
end
|
@@ -22,9 +35,9 @@ module ::RSpec
|
|
22
35
|
self
|
23
36
|
end
|
24
37
|
|
25
|
-
def with_named(
|
38
|
+
def with_named(*kwargs)
|
26
39
|
@args = []
|
27
|
-
@kwargs = kwargs
|
40
|
+
@kwargs = Hash[kwargs.map { [_1, _1] }]
|
28
41
|
self
|
29
42
|
end
|
30
43
|
|
@@ -13,6 +13,13 @@ module ::RSpec
|
|
13
13
|
def matches?(actual)
|
14
14
|
matches_for?(actual, :return)
|
15
15
|
end
|
16
|
+
|
17
|
+
def description
|
18
|
+
<<~TXT.gsub(/\n+/, " ")
|
19
|
+
to pass the arguments to the constructor of instance and return
|
20
|
+
the value returned by the instance method
|
21
|
+
TXT
|
22
|
+
end
|
16
23
|
end
|
17
24
|
end
|
18
25
|
end
|
@@ -15,6 +15,13 @@ module ::RSpec
|
|
15
15
|
|
16
16
|
matches_for?(actual, instance)
|
17
17
|
end
|
18
|
+
|
19
|
+
def description
|
20
|
+
<<~TXT.gsub(/\n+/, " ")
|
21
|
+
to pass the arguments to the constructor of instance, execute
|
22
|
+
provided instance method and return the instance of the class
|
23
|
+
TXT
|
24
|
+
end
|
18
25
|
end
|
19
26
|
end
|
20
27
|
end
|