slayer 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -0
- data/lib/slayer/command.rb +14 -6
- data/lib/slayer/rspec.rb +23 -0
- data/lib/slayer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8afcb532ebe21d444e89baf763a76b20cf4ab19ad1bf51d45bfbd29042f3ec16
|
4
|
+
data.tar.gz: 5f77a3a44acf2cd3a2888f38e67963b22866c5c81a44a628be1c28128d128812
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f383e130f4d8bef5d42f9de29db7890f585c8116f3f6ea093cb83fc3c8b29b608ee82b16e881130527adedaf36761e216985d8b13208abac93bfef74739d53c7
|
7
|
+
data.tar.gz: 2b96b3b9473ad470033138e8f201a6fc5cdc893c2724fa4e0de1a0c7e820af93256d91569fe67f2a36dc66191f0956cf5ba45c11394e6d7778d015ee2633e7cb
|
data/README.md
CHANGED
@@ -261,6 +261,39 @@ RSpec.describe RSpecCommand do
|
|
261
261
|
end
|
262
262
|
```
|
263
263
|
|
264
|
+
#### Stubbing Command Results
|
265
|
+
|
266
|
+
The RSpec helpers provide two utility functions for use in your tests which should simplify testing commands with stubbed results. This can be useful when you want
|
267
|
+
test a Rails controller, and your command is already tested separately. In this case, you only really care about the logic in your matching blocks --- not in the command itself.
|
268
|
+
|
269
|
+
Put another way: this is useful when you want to test the success or failure conditions of your commands.
|
270
|
+
|
271
|
+
```ruby
|
272
|
+
RSpec.describe FooController, type: :controller do do
|
273
|
+
context 'successful command' do
|
274
|
+
let(:foo) { create(:foo) }
|
275
|
+
let(:fake_res) { fake_result(ok: true, message: 'foo updated') }
|
276
|
+
|
277
|
+
describe '#update' do
|
278
|
+
# Foo will not be called, instead we will get back the stubbed response
|
279
|
+
# from the let block above, allowing us to bypass the command logic and
|
280
|
+
# test only the controller logic
|
281
|
+
stub_command_response(UpdateFooCommand, fake_res)
|
282
|
+
post :update, params: { id: foo.id }
|
283
|
+
expect(response).to have_http_status :ok
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
```
|
289
|
+
|
290
|
+
This method --- `stub_command_response` --- can take the return value as either a second argument, or as a block:
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
stub_command_response(UpdateFooCommand, fake_res) # => fake result as an argument
|
294
|
+
stub_command_response(UpdateFooCommand) { fake_res } # => fake result as a block
|
295
|
+
```
|
296
|
+
|
264
297
|
### Minitest
|
265
298
|
|
266
299
|
To use with Minitest, update your 'test_helper' file to include:
|
data/lib/slayer/command.rb
CHANGED
@@ -4,15 +4,11 @@ module Slayer
|
|
4
4
|
def call(*args, &block)
|
5
5
|
instance = self.new
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
rescue ResultFailureError => e
|
10
|
-
res = e.result
|
11
|
-
end
|
7
|
+
res = __get_result(instance, *args, &block)
|
8
|
+
handle_match(res, instance, block) if block_given?
|
12
9
|
|
13
10
|
raise CommandNotImplementedError unless res.is_a? Result
|
14
11
|
|
15
|
-
handle_match(res, instance, block) if block_given?
|
16
12
|
return res
|
17
13
|
end
|
18
14
|
ruby2_keywords :call if respond_to?(:ruby2_keywords, true)
|
@@ -30,6 +26,18 @@ module Slayer
|
|
30
26
|
raise ResultFailureError, err(value: value, status: status, message: message)
|
31
27
|
end
|
32
28
|
|
29
|
+
def __get_result(instance, *args, &block)
|
30
|
+
res = nil
|
31
|
+
|
32
|
+
begin
|
33
|
+
res = instance.call(*args, &block)
|
34
|
+
rescue ResultFailureError => e
|
35
|
+
res = e.result
|
36
|
+
end
|
37
|
+
|
38
|
+
res
|
39
|
+
end
|
40
|
+
|
33
41
|
private
|
34
42
|
|
35
43
|
def handle_match(res, instance, block)
|
data/lib/slayer/rspec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'rspec/mocks'
|
1
2
|
require 'rspec/expectations'
|
2
3
|
|
3
4
|
# rubocop:disable Metrics/BlockLength
|
@@ -92,3 +93,25 @@ RSpec::Matchers.define :be_err_result do
|
|
92
93
|
end
|
93
94
|
|
94
95
|
# rubocop:enable Metrics/BlockLength
|
96
|
+
|
97
|
+
module Slayer
|
98
|
+
module RspecHelpers
|
99
|
+
def stub_command_response(klass, res = nil, &block)
|
100
|
+
res = block.call if block_given? && res.nil?
|
101
|
+
|
102
|
+
expect(klass).to receive(:__get_result).and_return(res)
|
103
|
+
end
|
104
|
+
|
105
|
+
# rubocop:disable Naming/MethodParameterName
|
106
|
+
def fake_result(ok: true, value: nil, message: nil, status: nil)
|
107
|
+
res = Slayer::Result.new(value, status, message)
|
108
|
+
res.fail unless ok == true
|
109
|
+
|
110
|
+
res
|
111
|
+
end
|
112
|
+
|
113
|
+
# rubocop:enable Naming/MethodParameterName
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
RSpec.configure { |config| config.include Slayer::RspecHelpers }
|
data/lib/slayer/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slayer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wyatt Kirby
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-02-
|
12
|
+
date: 2023-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: virtus
|