tzu_mock 1.3.0 → 1.4.0

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: bfeba27435b14487d4f44b1a34dff18bab63068684ec8f29eab3bfc9fc8a613d
4
- data.tar.gz: 0340ffa48976774ba9472fc1564e490e753060bd674b2f69868b7e493aab04c6
3
+ metadata.gz: b451e46902e3dd97214eefcb8850032cda99c52994aa4f79a085ad9d55451064
4
+ data.tar.gz: 896fcfc6bdf7ad453ecfdbcc742e22e80c5f686f18ee197334477c6e112d03c6
5
5
  SHA512:
6
- metadata.gz: 57f6e5f426dbd6b9b109a032d27fc139b9dde8493b5e9677b39726add3f33428d5dc5f3e33c5b3d5a042204938331220877fd690fa663b1abb87e74e54539e4b
7
- data.tar.gz: bfdc27287313694416b94c56a6d787ec177fbc9abf1bab142dc94121c8fc945cb8e0b6722beb796236b3fdb72aeeade81967d4a65f84766ff823583b50302b01
6
+ metadata.gz: 8b5b594dba18bf9e53d3e13496f9f991b9561de558faccba09796ee52b7b2c6ae16ebe74dbf48f5cfd57883826b2998f6ac93fa96c9728c9052296384b7e353a
7
+ data.tar.gz: 65633578d6b4335c811698ba2adc9976e2a9f8fae4c5ffb0a5c2f41a56cd5333038426096e71f8beffc00dd7b9b1726d7611ae7c71e46a01ec7330aecc1db2ab
@@ -1,14 +1,21 @@
1
1
  module TzuMock
2
2
  module ClassMethods
3
- def method_missing(method, *args)
4
- return super(method) unless [:success, :invalid, :failure].include? method
3
+ MISSING_METHODS = %i[success invalid failure].freeze
4
+
5
+ def method_missing(method, *args, &block)
6
+ return super unless MISSING_METHODS.include? method
7
+
5
8
  prepare(method, *args)
6
9
  end
7
10
 
11
+ def respond_to_missing?(method_name, include_private = false)
12
+ MISSING_METHODS.include?(method_name) || super
13
+ end
14
+
8
15
  def prepare(type, klass, method = nil)
9
16
  # Get the rspec block context. Will not work if you call TzuMock#prepare directly.
10
17
  # Call TzuMock#success, TzuMock#invalid, or TzuMock#failure instead
11
- rspec_context = binding.of_caller(2).eval('self')
18
+ rspec_context = binding.of_caller(2).eval("self")
12
19
 
13
20
  Mocker.new(type, klass, rspec_context, method).mock
14
21
  end
@@ -19,7 +19,7 @@ module TzuMock
19
19
 
20
20
  # Need to pass variables in explicity to give the Proc access to them
21
21
  def mock_proc(klass, methods, success, result, type)
22
- Proc.new do
22
+ proc do
23
23
  methods.each do |method|
24
24
  allow(klass).to receive(method) do |&block|
25
25
  outcome = Tzu::Outcome.new(success, result, type)
@@ -48,7 +48,7 @@ module TzuMock
48
48
  when :failure
49
49
  :execution
50
50
  else
51
- raise ArgumentError.new('Invalid type, must be :success, :invalid, or :failure')
51
+ raise ArgumentError.new("Invalid type, must be :success, :invalid, or :failure")
52
52
  end
53
53
  end
54
54
  end
@@ -1,4 +1,4 @@
1
- require 'hashie'
1
+ require "hashie"
2
2
 
3
3
  module TzuMock
4
4
  class HashResult < Hash
@@ -27,7 +27,7 @@ module TzuMock
27
27
  end
28
28
 
29
29
  def build_array(result)
30
- result.map { |r| r.is_a?(Hash) ? build_hash(r) : r}
30
+ result.map { |r| r.is_a?(Hash) ? build_hash(r) : r }
31
31
  end
32
32
  end
33
33
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TzuMock
4
+ VERSION = "1.4.0"
5
+ end
data/lib/tzu_mock.rb CHANGED
@@ -1,9 +1,10 @@
1
- require 'binding_of_caller'
2
- require 'tzu_mock/class_methods'
3
- require 'tzu_mock/result_builder'
4
- require 'tzu_mock/mocker'
5
- require 'tzu_mock/configuration'
1
+ require "binding_of_caller"
2
+ require "tzu_mock/class_methods"
3
+ require "tzu_mock/result_builder"
4
+ require "tzu_mock/mocker"
5
+ require "tzu_mock/configuration"
6
6
 
7
7
  module TzuMock
8
- extend ClassMethods, Configuration
8
+ extend Configuration
9
+ extend ClassMethods
9
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzu_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-16 00:00:00.000000000 Z
11
+ date: 2024-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -45,7 +45,7 @@ dependencies:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -59,35 +59,7 @@ dependencies:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: byebug
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rake
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
62
+ type: :runtime
91
63
  prerelease: false
92
64
  version_requirements: !ruby/object:Gem::Requirement
93
65
  requirements:
@@ -107,13 +79,12 @@ files:
107
79
  - lib/tzu_mock/configuration.rb
108
80
  - lib/tzu_mock/mocker.rb
109
81
  - lib/tzu_mock/result_builder.rb
110
- - spec/spec_helper.rb
111
- - spec/tzu_mock_spec.rb
82
+ - lib/tzu_mock/version.rb
112
83
  homepage: https://github.com/onfido/tzu_mock
113
84
  licenses:
114
85
  - MIT
115
86
  metadata: {}
116
- post_install_message:
87
+ post_install_message:
117
88
  rdoc_options: []
118
89
  require_paths:
119
90
  - lib
@@ -128,10 +99,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
99
  - !ruby/object:Gem::Version
129
100
  version: '0'
130
101
  requirements: []
131
- rubygems_version: 3.0.3.1
132
- signing_key:
102
+ rubygems_version: 3.4.22
103
+ signing_key:
133
104
  specification_version: 4
134
105
  summary: TDD with Tzu!
135
- test_files:
136
- - spec/spec_helper.rb
137
- - spec/tzu_mock_spec.rb
106
+ test_files: []
data/spec/spec_helper.rb DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'tzu_mock'
4
-
5
- RSpec.configure do |config|
6
- # Enable flags like --only-failures and --next-failure
7
- config.example_status_persistence_file_path = ".rspec_status"
8
-
9
- # Disable RSpec exposing methods globally on `Module` and `main`
10
- config.disable_monkey_patching!
11
-
12
- config.expect_with :rspec do |c|
13
- c.syntax = :expect
14
- end
15
- end
@@ -1,210 +0,0 @@
1
- require 'tzu'
2
-
3
- RSpec.describe TzuMock do
4
- let(:result) { 'Desired Result' }
5
- let(:error) { { error: 'ERROR' } }
6
- let(:params) { { last_name: 'Turner' } }
7
-
8
- class UpdateUser
9
- include Tzu
10
- include Tzu::Validation
11
-
12
- def call(params)
13
- raise ArgumentError.new('I should be mocked!')
14
- end
15
- end
16
-
17
- class MockController
18
- attr_reader :method_called
19
-
20
- def update(params = {})
21
- UpdateUser.run(params) do
22
- success do |result|
23
- @method_called = :success
24
- end
25
-
26
- invalid do |error|
27
- @method_called = :invalid
28
- end
29
-
30
- failure do |error|
31
- @method_called = :failure
32
- end
33
- end
34
- end
35
- end
36
-
37
- context 'configuration' do
38
- context 'when no stub methods have been added' do
39
- it 'configures the default stub methods' do
40
- expect(TzuMock.configuration.stub_methods).to eq TzuMock::Config::DEFAULT_MOCK_METHODS
41
- end
42
- end
43
-
44
- context 'when stub methods have been added' do
45
- let(:stub_methods) { [:go, :go!] }
46
-
47
- before do
48
- TzuMock.configure { |config| config.stub_methods = stub_methods }
49
- end
50
-
51
- it 'appends the configured methods to the default stub methods' do
52
- expect(TzuMock.configuration.stub_methods.sort).to eq (TzuMock::Config::DEFAULT_MOCK_METHODS + stub_methods).sort
53
- end
54
- end
55
- end
56
-
57
- context 'result builder' do
58
- let(:outcome) { UpdateUser.run!(params) }
59
-
60
- before { TzuMock.success(UpdateUser).returns(result) }
61
-
62
- context 'hash' do
63
- let(:result) { {name: 'me!'} }
64
- it 'returns hash attributes as methods' do
65
- expect(outcome.result.is_a?(Hash)).to be_truthy
66
- expect(outcome.result.name).to eq(result[:name])
67
- end
68
- end
69
- context 'array' do
70
- context 'contains hash' do
71
- let(:result) { [{name: 'other'}] }
72
- it 'responds to name' do
73
- expect(outcome.result.first.is_a?(Hash)).to be_truthy
74
- expect(outcome.result.first.name).to eq(result.first[:name])
75
- end
76
- end
77
- context 'does not ctonain hash' do
78
- let(:result) { ['me'] }
79
- it 'responds to name' do
80
- expect(outcome.result.first.is_a?(String)).to be_truthy
81
- expect(outcome.result).to eq(result)
82
- end
83
- end
84
- end
85
- context 'string' do
86
- let(:result) { 'me!' }
87
- it 'responds to name' do
88
- expect(outcome.result.is_a?(String)).to be_truthy
89
- expect(outcome.result).to eq(result)
90
- end
91
- end
92
- end
93
-
94
- context 'when invocation does not have a block' do
95
- context 'success' do
96
- before { TzuMock.success(UpdateUser).returns(result) }
97
-
98
- let(:outcome) { UpdateUser.run!(params) }
99
-
100
- it 'mocks a successful outcome and allows parameters to be verified' do
101
- expect(outcome.success?).to be true
102
- expect(outcome.result).to eq result
103
- expect(outcome.type).to be nil
104
- expect(UpdateUser).to have_received(:run!).with(params)
105
- end
106
- end
107
-
108
- context 'invalid' do
109
- before { TzuMock.invalid(UpdateUser).returns(error) }
110
-
111
- let(:outcome) { UpdateUser.run!(params) }
112
-
113
- it 'mocks an invalid outcome and allows parameters to be verified' do
114
- expect(outcome.success?).to be false
115
- expect(outcome.result).to eq error
116
- expect(outcome.type).to eq :validation
117
- expect(UpdateUser).to have_received(:run!).with(params)
118
- end
119
- end
120
-
121
- context 'failure' do
122
- before { TzuMock.failure(UpdateUser).returns(error) }
123
-
124
- let(:outcome) { UpdateUser.run(params) }
125
-
126
- it 'mocks a failed outcome and allows parameters to be verified' do
127
- expect(outcome.success?).to be false
128
- expect(outcome.result).to eq error
129
- expect(outcome.type).to eq :execution
130
- expect(UpdateUser).to have_received(:run).with(params)
131
- end
132
- end
133
- end
134
-
135
- context 'when invocation has a block' do
136
- context 'success' do
137
- before { TzuMock.success(UpdateUser).returns(result) }
138
-
139
- let(:controller) { MockController.new }
140
-
141
- it 'mocks a successful outcome and allows parameters to be verified' do
142
- controller.update(params)
143
- expect(UpdateUser).to have_received(:run).with(params)
144
- expect(controller.method_called).to eq :success
145
- end
146
- end
147
-
148
- context 'invalid' do
149
- before { TzuMock.invalid(UpdateUser).returns(error) }
150
-
151
- let(:controller) { MockController.new }
152
-
153
- it 'mocks a successful outcome and allows parameters to be verified' do
154
- controller.update(params)
155
- expect(UpdateUser).to have_received(:run).with(params)
156
- expect(controller.method_called).to eq :invalid
157
- end
158
- end
159
-
160
- context 'failure' do
161
- before { TzuMock.failure(UpdateUser).returns(error) }
162
-
163
- let(:controller) { MockController.new }
164
-
165
- it 'mocks a successful outcome and allows parameters to be verified' do
166
- controller.update(params)
167
- expect(UpdateUser).to have_received(:run).with(params)
168
- expect(controller.method_called).to eq :failure
169
- end
170
- end
171
- end
172
-
173
- context 'TzuMock receive no return result' do
174
- context 'success' do
175
- before { TzuMock.success(UpdateUser) }
176
-
177
- let(:outcome) { UpdateUser.run!(params) }
178
-
179
- it 'mocks a successful outcome and allows parameters to be verified' do
180
- expect(outcome.success?).to be true
181
- expect(outcome.type).to be nil
182
- expect(UpdateUser).to have_received(:run!).with(params)
183
- end
184
- end
185
-
186
- context 'invalid' do
187
- before { TzuMock.invalid(UpdateUser) }
188
-
189
- let(:outcome) { UpdateUser.run!(params) }
190
-
191
- it 'mocks an invalid outcome and allows parameters to be verified' do
192
- expect(outcome.success?).to be false
193
- expect(outcome.type).to eq :validation
194
- expect(UpdateUser).to have_received(:run!).with(params)
195
- end
196
- end
197
-
198
- context 'failure' do
199
- before { TzuMock.failure(UpdateUser) }
200
-
201
- let(:outcome) { UpdateUser.run(params) }
202
-
203
- it 'mocks a failed outcome and allows parameters to be verified' do
204
- expect(outcome.success?).to be false
205
- expect(outcome.type).to eq :execution
206
- expect(UpdateUser).to have_received(:run).with(params)
207
- end
208
- end
209
- end
210
- end