tzu_mock 1.1.0 → 1.2.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
  SHA1:
3
- metadata.gz: b40c2156219f3cfc8999af245f2ae622f440d667
4
- data.tar.gz: cd0617ab6631d769609f1bd191d07402ba69c1ec
3
+ metadata.gz: add3ea6e0b5d294529170a40e2cebd3b9f7c4e08
4
+ data.tar.gz: 1f56ebf946f95c2ac49c3d1c0554ed3a0da51e06
5
5
  SHA512:
6
- metadata.gz: 2be08f3868d3d17d1a596a551e446e83bf0f65ce48191534855306dfde0b4ae888ab1112bdcaa84e815ecc7433ed409a572f9b0d4efc4b2dcdd5076519200f5b
7
- data.tar.gz: 153168acbfa9f3e9c9ebd9ca8bef116d5d9392b282bd761e2ced532fbafb250f290a0a73376b442bfb26fd7ae70380391d581cf4994e1944d696fdf90530adce
6
+ metadata.gz: 813afb83eaee56ea9a570f22081752872505ec4d6240bfd7e51b61eb2628eefd1a7a1ea246ac8f423e03dc12cf5779e2d9c50f60ed15380eadbac6fe8792cd43
7
+ data.tar.gz: 33acb4cd26ae3ff5b8154c76c64f9e9aa25d85ae2c37653dcf81081b096708cbd98ce786acd55e02bb5a4c4a455163c0dc50eec9ddc046ef1ef29f8454f82764
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Tzu Mock
1
+ # Tzu Mock [![Build Status](https://travis-ci.org/onfido/tzu_mock.svg)](https://travis-ci.org/onfido/tzu_mock)
2
2
 
3
3
  A very simple library for mocking Tzu in RSpec
4
4
 
@@ -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,
@@ -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
 
@@ -10,8 +10,9 @@ module TzuMock
10
10
  end
11
11
 
12
12
  def returns(result)
13
- @result = result
13
+ @result = ResultBuilder.build(result)
14
14
  mock
15
+ self
15
16
  end
16
17
 
17
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
@@ -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) }
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-09 00:00:00.000000000 Z
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -24,20 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.4.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: tzu
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: 0.0.1.0
47
+ version: 0.0.1
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: 0.0.1.0
54
+ version: 0.0.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,34 @@ 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'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jeweler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  description: Simple library for mocking Tzu in RSpec
70
112
  email: mail@blakewilliamturner.com
71
113
  executables: []
@@ -78,6 +120,7 @@ files:
78
120
  - lib/tzu_mock/class_methods.rb
79
121
  - lib/tzu_mock/configuration.rb
80
122
  - lib/tzu_mock/mocker.rb
123
+ - lib/tzu_mock/result_builder.rb
81
124
  - spec/spec_helper.rb
82
125
  - spec/tzu_mock_spec.rb
83
126
  homepage: https://github.com/onfido/tzu_mock
@@ -100,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
143
  version: '0'
101
144
  requirements: []
102
145
  rubyforge_project:
103
- rubygems_version: 2.4.3
146
+ rubygems_version: 2.2.2
104
147
  signing_key:
105
148
  specification_version: 4
106
149
  summary: TDD with Tzu!