slayer 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f88c862cf4596840150934c88372a6cda656557e34da5a3918e98f19b159c645
4
- data.tar.gz: 84d431982bad263f5a8cad99e3cd7fdb62610b9b75a80621c8ad457d01f93600
3
+ metadata.gz: 2616136d45efa56080f52cc1e50c83234aef62ea9d17d3c710fdbda46640a4ab
4
+ data.tar.gz: 6662a50c7491c23c48b17835b0ab12ceeed09fa58fc9c450d7a0371f2e492154
5
5
  SHA512:
6
- metadata.gz: e390d010a072ec169f4c8a776547a4e3d562599db94a11a91534c7f0a2cc0667ce10c94fcfe765de658edb1bd477f38a4ac14b1972cbc183c03c666391193fbf
7
- data.tar.gz: 13adcd8d46c0e28e1f4c243426ca1ad628157ca056d8298932dbdbf946d288661358a1c6e2a6ebbcf2e497a9efea45811579cb6063c28e46e80410f1a6f6d0e4
6
+ metadata.gz: 768fac163259ffd9b3c81707f76293da2a5df147639558257a8703824c7605d30761668b670cf105639542f75b6e8f77b9b2b7dd741a9bc0460727c5e6b20d03
7
+ data.tar.gz: ffa8cee897239bedb5d1d0bbb19b57cd931c74aee55921e301c92cf557e2e9816c9705ef5d78ea1d4c84b91e55dff4eeac3c8b1a012688c47dcf8e159500cd56
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:
@@ -4,15 +4,11 @@ module Slayer
4
4
  def call(*args, &block)
5
5
  instance = self.new
6
6
 
7
- begin
8
- res = instance.call(*args, &block)
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)
@@ -26,10 +22,24 @@ module Slayer
26
22
  end
27
23
 
28
24
  def err!(value: nil, status: :default, message: nil)
29
- warn '[DEPRECATION] `err!` is deprecated. Please use `return err` instead.'
25
+ unless ENV['SUPPRESS_SLAYER_WARNINGS']
26
+ warn '[DEPRECATION] `err!` is deprecated. Please use `return err` instead.'
27
+ end
30
28
  raise ResultFailureError, err(value: value, status: status, message: message)
31
29
  end
32
30
 
31
+ def __get_result(instance, *args, &block)
32
+ res = nil
33
+
34
+ begin
35
+ res = instance.call(*args, &block)
36
+ rescue ResultFailureError => e
37
+ res = e.result
38
+ end
39
+
40
+ res
41
+ end
42
+
33
43
  private
34
44
 
35
45
  def handle_match(res, instance, block)
@@ -4,17 +4,19 @@ module Slayer
4
4
  class Command
5
5
  class << self
6
6
  def pass(value: nil, status: :default, message: nil)
7
- warn '[DEPRECATION] `pass` is deprecated. Please use `ok` instead.'
7
+ warn '[DEPRECATION] `pass` is deprecated. Please use `ok` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
8
8
  ok(value: value, status: status, message: message)
9
9
  end
10
10
 
11
11
  def flunk(value: nil, status: :default, message: nil)
12
- warn '[DEPRECATION] `flunk` is deprecated. Please use `err` instead.'
12
+ warn '[DEPRECATION] `flunk` is deprecated. Please use `err` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
13
13
  err(value: value, status: status, message: message)
14
14
  end
15
15
 
16
16
  def flunk!(value: nil, status: :default, message: nil)
17
- warn '[DEPRECATION] `flunk!` is deprecated. Please use `return err` instead.'
17
+ unless ENV['SUPPRESS_SLAYER_WARNINGS']
18
+ warn '[DEPRECATION] `flunk!` is deprecated. Please use `return err` instead.'
19
+ end
18
20
  err!(value: value, status: status, message: message)
19
21
  end
20
22
  end
@@ -26,24 +28,24 @@ module Slayer
26
28
 
27
29
  class Result
28
30
  def success?
29
- warn '[DEPRECATION] `success?` is deprecated. Please use `ok?` instead.'
31
+ warn '[DEPRECATION] `success?` is deprecated. Please use `ok?` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
30
32
  ok?
31
33
  end
32
34
 
33
35
  def failure?
34
- warn '[DEPRECATION] `failure?` is deprecated. Please use `err?` instead.'
36
+ warn '[DEPRECATION] `failure?` is deprecated. Please use `err?` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
35
37
  err?
36
38
  end
37
39
  end
38
40
 
39
41
  class ResultMatcher
40
42
  def pass(...)
41
- warn '[DEPRECATION] `pass` is deprecated. Please use `ok` instead.'
43
+ warn '[DEPRECATION] `pass` is deprecated. Please use `ok` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
42
44
  ok(...)
43
45
  end
44
46
 
45
47
  def fail(...)
46
- warn '[DEPRECATION] `fail` is deprecated. Please use `err` instead.'
48
+ warn '[DEPRECATION] `fail` is deprecated. Please use `err` instead.' unless ENV['SUPPRESS_SLAYER_WARNINGS']
47
49
  err(...)
48
50
  end
49
51
  end
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 }
@@ -1,3 +1,3 @@
1
1
  module Slayer
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.3'
3
3
  end
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.1
4
+ version: 0.5.3
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-03 00:00:00.000000000 Z
12
+ date: 2023-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus