tzu_mock 1.0.0 → 1.3.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
- SHA1:
3
- metadata.gz: a623fe290d3bf0e2ecaeda8f7891272261115576
4
- data.tar.gz: 5408759d91670c24cd7e3660f60ea83ac7fd9b5f
2
+ SHA256:
3
+ metadata.gz: bfeba27435b14487d4f44b1a34dff18bab63068684ec8f29eab3bfc9fc8a613d
4
+ data.tar.gz: 0340ffa48976774ba9472fc1564e490e753060bd674b2f69868b7e493aab04c6
5
5
  SHA512:
6
- metadata.gz: ec7f5e5796be2ec05e37b94138d8e9cf66712e9fe989727dedb4da04688d7d2cc8477c35b3321216c71ba2c71fd7671c478db406bdd822f844ee783680d95e96
7
- data.tar.gz: 85e762fa6f101ae45d7fbcf540cb3ed0ea7f08a8d4bcc2f305ac36bb68b73a39e9bf2bab705c89d7ca3d6e8f0944d2fd73c0317aed6b2e3153f431df2bd89f3f
6
+ metadata.gz: 57f6e5f426dbd6b9b109a032d27fc139b9dde8493b5e9677b39726add3f33428d5dc5f3e33c5b3d5a042204938331220877fd690fa663b1abb87e74e54539e4b
7
+ data.tar.gz: bfdc27287313694416b94c56a6d787ec177fbc9abf1bab142dc94121c8fc945cb8e0b6722beb796236b3fdb72aeeade81967d4a65f84766ff823583b50302b01
data/README.md CHANGED
@@ -144,6 +144,27 @@ end
144
144
 
145
145
  TzuMock effortlessly passes your desired outcome to the appropriate block.
146
146
 
147
+ ## Hash Result
148
+
149
+ TzuMock converts your result hash attributes into methods by default.
150
+
151
+ ```ruby
152
+ # if you return this result
153
+ before { TzuMock.success(UpdateUser).returns({name: 'me'}) }
154
+
155
+ # you can access it on this way
156
+ outcome = UpdateUser.run(params)
157
+ outcome.result.name
158
+
159
+ # even if you have result as array contains hashs
160
+ before { TzuMock.success(UpdateUser).returns([{name: 'me'}]) }
161
+
162
+ # you can access it on this way
163
+ outcome = UpdateUser.run(params)
164
+ outcome.result.first.name
165
+
166
+ ```
167
+
147
168
  ## Configuration
148
169
 
149
170
  By default, TzuMock mocks the `run` and `run!` methods,
@@ -10,7 +10,7 @@ module TzuMock
10
10
  # Call TzuMock#success, TzuMock#invalid, or TzuMock#failure instead
11
11
  rspec_context = binding.of_caller(2).eval('self')
12
12
 
13
- Mocker.new(type, klass, rspec_context, method)
13
+ Mocker.new(type, klass, rspec_context, method).mock
14
14
  end
15
15
  end
16
16
  end
@@ -6,11 +6,13 @@ module TzuMock
6
6
 
7
7
  def mock
8
8
  @rspec_context.instance_eval(&mock_proc(@klass, mock_methods, success?, @result, error_type))
9
+ self
9
10
  end
10
11
 
11
12
  def returns(result)
12
- @result = result
13
+ @result = ResultBuilder.build(result)
13
14
  mock
15
+ self
14
16
  end
15
17
 
16
18
  private
@@ -0,0 +1,34 @@
1
+ require 'hashie'
2
+
3
+ module TzuMock
4
+ class HashResult < Hash
5
+ include Hashie::Extensions::MethodAccess
6
+
7
+ def initialize(attributes)
8
+ update(attributes)
9
+ end
10
+ end
11
+
12
+ class ResultBuilder
13
+ class << self
14
+ def build(result)
15
+ name = "build_#{result.class.name.downcase}".to_sym
16
+ if respond_to?(name, true)
17
+ send(name, result)
18
+ else
19
+ result
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def build_hash(result)
26
+ HashResult.new(result)
27
+ end
28
+
29
+ def build_array(result)
30
+ result.map { |r| r.is_a?(Hash) ? build_hash(r) : r}
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/tzu_mock.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'binding_of_caller'
2
2
  require 'tzu_mock/class_methods'
3
+ require 'tzu_mock/result_builder'
3
4
  require 'tzu_mock/mocker'
4
5
  require 'tzu_mock/configuration'
5
6
 
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,15 @@
1
- require 'rubygems'
2
- require 'byebug'
3
- require 'tzu'
4
- require 'rspec'
5
- require 'bundler/setup'
6
- Bundler.setup
1
+ # frozen_string_literal: true
7
2
 
8
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,6 +1,6 @@
1
- require 'spec_helper'
1
+ require 'tzu'
2
2
 
3
- describe TzuMock do
3
+ RSpec.describe TzuMock do
4
4
  let(:result) { 'Desired Result' }
5
5
  let(:error) { { error: 'ERROR' } }
6
6
  let(:params) { { last_name: 'Turner' } }
@@ -54,6 +54,43 @@ describe TzuMock do
54
54
  end
55
55
  end
56
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
+
57
94
  context 'when invocation does not have a block' do
58
95
  context 'success' do
59
96
  before { TzuMock.success(UpdateUser).returns(result) }
@@ -132,4 +169,42 @@ describe TzuMock do
132
169
  end
133
170
  end
134
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
135
210
  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.0.0
4
+ version: 1.3.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: 2015-07-12 00:00:00.000000000 Z
11
+ date: 2022-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -25,33 +25,47 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.7'
27
27
  - !ruby/object:Gem::Dependency
28
- name: tzu
28
+ name: hashie
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.0.1.0
34
- type: :development
33
+ version: '3'
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.0.1.0
40
+ version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tzu
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: 2.4.0
61
+ version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: 2.4.0
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: byebug
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
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
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Simple library for mocking Tzu in RSpec
70
98
  email: mail@blakewilliamturner.com
71
99
  executables: []
@@ -78,13 +106,14 @@ files:
78
106
  - lib/tzu_mock/class_methods.rb
79
107
  - lib/tzu_mock/configuration.rb
80
108
  - lib/tzu_mock/mocker.rb
109
+ - lib/tzu_mock/result_builder.rb
81
110
  - spec/spec_helper.rb
82
111
  - spec/tzu_mock_spec.rb
83
112
  homepage: https://github.com/onfido/tzu_mock
84
113
  licenses:
85
114
  - MIT
86
115
  metadata: {}
87
- post_install_message:
116
+ post_install_message:
88
117
  rdoc_options: []
89
118
  require_paths:
90
119
  - lib
@@ -99,12 +128,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
128
  - !ruby/object:Gem::Version
100
129
  version: '0'
101
130
  requirements: []
102
- rubyforge_project:
103
- rubygems_version: 2.2.2
104
- signing_key:
131
+ rubygems_version: 3.0.3.1
132
+ signing_key:
105
133
  specification_version: 4
106
134
  summary: TDD with Tzu!
107
135
  test_files:
108
136
  - spec/spec_helper.rb
109
137
  - spec/tzu_mock_spec.rb
110
- has_rdoc: