active_mocker 1.1.6 → 1.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.hound.yml +0 -0
- data/README.md +14 -12
- data/lib/active_hash/ar_api.rb +2 -0
- data/lib/active_hash/init.rb +33 -0
- data/lib/active_mocker.rb +3 -2
- data/lib/active_mocker/base.rb +129 -31
- data/lib/active_mocker/public_methods.rb +11 -0
- data/lib/active_mocker/schema_reader.rb +2 -0
- data/lib/active_mocker/version.rb +1 -1
- data/spec/lib/active_mocker/base_spec.rb +68 -16
- data/spec/lib/active_mocker/model_reader_spec.rb +1 -0
- data/spec/lib/active_mocker/schema_reader_spec.rb +3 -3
- data/spec/lib/compare_mocker_and_record_spec.rb +11 -46
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fb5d2c18e0123bbfcd1e6e34e9ce6f323a29324
|
4
|
+
data.tar.gz: dbe134929a21a543e9f72ea5ff773bcbb1601f97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1edf9a047d35d844d8382192dce7e368afe325a86adaa825196a4d30082d9171998bb3dcc37e9e97aea70e3fde04a89e95dee15f77a88154367b75d51060382c
|
7
|
+
data.tar.gz: 20bc1b6439aacc5de848ea444463d9477cbf567a4b67f860f5618a2a7726e1800a8717feda70fe0074b7ecb30a5497778158bdb41f0492209f1f11c5c968560d
|
data/.hound.yml
ADDED
File without changes
|
data/README.md
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
|
4
4
|
Create mocks from active record models without loading rails or running a database.
|
5
5
|
|
6
|
-
|
7
6
|
## Installation
|
8
7
|
|
9
8
|
Add this line to your application's Gemfile:
|
@@ -51,7 +50,7 @@ Or install it yourself as:
|
|
51
50
|
|
52
51
|
require 'active_mocker'
|
53
52
|
|
54
|
-
ActiveMocker
|
53
|
+
ActiveMocker.configure do |config|
|
55
54
|
# Required Options
|
56
55
|
config.schema_file = "#{APP_ROOT}/db/schema.rb"
|
57
56
|
config.model_dir = "#{APP_ROOT}/app/models"
|
@@ -65,13 +64,13 @@ Or install it yourself as:
|
|
65
64
|
config.log_level = Logger::WARN #default
|
66
65
|
end
|
67
66
|
|
68
|
-
ActiveMocker
|
67
|
+
ActiveMocker.mock('Person')
|
69
68
|
=> PersonMock
|
70
69
|
|
71
70
|
PersonMock.column_names
|
72
71
|
=> ["account_id", "first_name", "last_name", "address", "city"]
|
73
72
|
|
74
|
-
person_mock = PersonMock.new(first_name: "Dustin", last_name: "Zeisler", account:
|
73
|
+
person_mock = PersonMock.new(first_name: "Dustin", last_name: "Zeisler", account: ActiveMocker.mock('Account').new)
|
75
74
|
=> #<PersonMock @first_name="Dustin", @last_name="Zeisler">
|
76
75
|
|
77
76
|
person_mock.first_name
|
@@ -106,14 +105,14 @@ Or install it yourself as:
|
|
106
105
|
=> RuntimeError: #bar is not Implemented for Class: PersonMock
|
107
106
|
|
108
107
|
|
109
|
-
|
108
|
+
person_mock.mock_instance_method(:bar) do |name, type=nil|
|
110
109
|
"Now implemented with #{name} and #{type}"
|
111
110
|
end
|
112
111
|
|
113
|
-
|
112
|
+
person_mock.new.bar('foo', 'type')
|
114
113
|
=> "Now implemented with foo and type"
|
115
114
|
|
116
|
-
|
115
|
+
person_mock.mock_class_method(:baz) do
|
117
116
|
"Now implemented"
|
118
117
|
end
|
119
118
|
|
@@ -130,7 +129,7 @@ Or install it yourself as:
|
|
130
129
|
|
131
130
|
end
|
132
131
|
|
133
|
-
|
132
|
+
person_mock.new.bar('foo', 'type')
|
134
133
|
=> ArgumentError: wrong number of arguments (2 for 1)
|
135
134
|
|
136
135
|
|
@@ -145,7 +144,7 @@ Or install it yourself as:
|
|
145
144
|
|
146
145
|
end
|
147
146
|
|
148
|
-
|
147
|
+
person_mock.mock_instance_method(:bar) do |name, type=nil|
|
149
148
|
"Now implemented with #{name} and #{type}"
|
150
149
|
end
|
151
150
|
=> NameError: undefined method `bar' for class `PersonMock'
|
@@ -155,11 +154,11 @@ ActiveHash is a simple base class that allows you to use a ruby hash as a readon
|
|
155
154
|
[zilkey/active_hash](https://github.com/zilkey/active_hash)
|
156
155
|
|
157
156
|
|
158
|
-
ActiveMocker
|
157
|
+
ActiveMocker.configure do |config|
|
159
158
|
config.active_hash_as_base = true
|
160
159
|
end
|
161
160
|
|
162
|
-
ActiveMocker
|
161
|
+
ActiveMocker.mock('Person').superclass
|
163
162
|
=> ActiveHash::Base
|
164
163
|
|
165
164
|
dustin = PersonMock.create(first_name: 'Dustin')
|
@@ -174,7 +173,7 @@ ActiveHash is a simple base class that allows you to use a ruby hash as a readon
|
|
174
173
|
dustin.save
|
175
174
|
=> true
|
176
175
|
|
177
|
-
### Additional ActiveHash
|
176
|
+
### Additional ActiveHash extensions for matching ActiveRecord
|
178
177
|
|
179
178
|
|
180
179
|
#### #update method
|
@@ -209,6 +208,9 @@ ActiveHash is a simple base class that allows you to use a ruby hash as a readon
|
|
209
208
|
* **::mock** model names and table names must follow the default ActiveRecord naming pattern.
|
210
209
|
* Included/extended module methods will not be included on the mock.
|
211
210
|
|
211
|
+
## Inspiration
|
212
|
+
Thanks to Jeff Olfert for being my original inspiration for this project.
|
213
|
+
|
212
214
|
## Contributing
|
213
215
|
|
214
216
|
1. Fork it ( http://github.com/zeisler/active_mocker/fork )
|
data/lib/active_hash/ar_api.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
require_relative 'destroy_all'
|
2
2
|
require_relative 'update'
|
3
3
|
require_relative 'find_by'
|
4
|
+
require_relative 'init'
|
4
5
|
|
5
6
|
module ActiveHash
|
6
7
|
|
7
8
|
module ARApi
|
8
9
|
|
9
10
|
include ARApi::Update
|
11
|
+
include ARApi::Init
|
10
12
|
|
11
13
|
def self.included(base)
|
12
14
|
base.extend(ClassMethods)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
module ActiveHash
|
3
|
+
|
4
|
+
module ARApi
|
5
|
+
|
6
|
+
module Init
|
7
|
+
|
8
|
+
attr_reader :associations
|
9
|
+
|
10
|
+
def initialize(attributes = {})
|
11
|
+
filter_associations(HashWithIndifferentAccess.new(attributes))
|
12
|
+
@attributes.dup.merge(@associations.dup).each do |key, value|
|
13
|
+
send "#{key}=", value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def filter_associations(attributes)
|
20
|
+
@attributes = attributes.select do |k, v|
|
21
|
+
self.class.send(:attribute_names).include? k.to_sym
|
22
|
+
end
|
23
|
+
@attributes = self.class.send(:attribute_template).merge(@attributes)
|
24
|
+
@associations = attributes.select do |k, v|
|
25
|
+
self.class.send(:association_names).include? k.to_sym
|
26
|
+
end
|
27
|
+
@associations = self.class.send(:association_template).merge(associations)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
data/lib/active_mocker.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require "active_mocker/version"
|
2
2
|
$:.unshift File.expand_path('../../', __FILE__)
|
3
|
+
require 'singleton'
|
3
4
|
require 'logger'
|
4
5
|
require 'active_mocker/logger'
|
5
6
|
require 'active_support/all'
|
6
7
|
require 'active_mocker/table'
|
7
8
|
require 'active_mocker/field'
|
8
9
|
require 'file_reader'
|
10
|
+
require 'active_mocker/public_methods'
|
9
11
|
require 'active_mocker/config'
|
10
12
|
require 'active_mocker/reparameterize'
|
11
13
|
require 'active_mocker/active_record'
|
@@ -17,6 +19,5 @@ require 'active_mocker/active_record'
|
|
17
19
|
require 'active_mocker/model_reader'
|
18
20
|
require 'active_mocker/reparameterize'
|
19
21
|
require 'active_hash/ar_api'
|
20
|
-
module ActiveMocker
|
21
22
|
|
22
|
-
|
23
|
+
|
data/lib/active_mocker/base.rb
CHANGED
@@ -14,7 +14,6 @@ module ActiveMocker
|
|
14
14
|
:model_file_reader,
|
15
15
|
:schema_file_reader
|
16
16
|
|
17
|
-
|
18
17
|
attr_reader :model_name, :klass
|
19
18
|
|
20
19
|
def initialize(model_name)
|
@@ -28,7 +27,7 @@ module ActiveMocker
|
|
28
27
|
end
|
29
28
|
|
30
29
|
def self.mock(model_name)
|
31
|
-
self.new
|
30
|
+
self.send(:new, model_name).klass
|
32
31
|
end
|
33
32
|
|
34
33
|
def model_definition
|
@@ -48,56 +47,76 @@ module ActiveMocker
|
|
48
47
|
end
|
49
48
|
|
50
49
|
def active_hash_mock_class
|
51
|
-
|
52
|
-
add_column_names_method
|
50
|
+
fill_templates
|
53
51
|
klass = create_klass
|
54
|
-
fields = table_definition.column_names
|
52
|
+
fields = table_definition.column_names
|
55
53
|
klass.class_eval do
|
56
54
|
klass.fields(*fields)
|
57
55
|
end
|
58
56
|
|
57
|
+
add_relationships_methods
|
58
|
+
add_column_names_method
|
59
59
|
add_method_mock_of
|
60
60
|
if model_methods
|
61
61
|
add_class_methods
|
62
62
|
add_instance_methods
|
63
63
|
end
|
64
|
-
|
65
64
|
end
|
66
65
|
|
67
66
|
def plain_mock_class
|
67
|
+
fill_templates
|
68
68
|
add_method_mock_of
|
69
69
|
if model_methods
|
70
70
|
add_class_methods
|
71
71
|
add_instance_methods
|
72
72
|
end
|
73
|
-
add_relationships if model_relationships
|
74
|
-
add_column_names_method if schema_attributes
|
75
73
|
add_table_attributes if schema_attributes
|
74
|
+
add_relationships_methods if model_relationships
|
75
|
+
add_column_names_method if schema_attributes
|
76
76
|
create_initializer if mass_assignment
|
77
77
|
end
|
78
78
|
|
79
79
|
def create_initializer
|
80
80
|
klass = create_klass
|
81
|
-
klass.
|
82
|
-
|
83
|
-
options.each {|method, value|
|
81
|
+
klass.class_eval <<-'eos', __FILE__, __LINE__+1
|
82
|
+
def initialize(options={})
|
83
|
+
options.each {|method, value| write_attribute(method, value) }
|
84
84
|
end
|
85
|
-
|
85
|
+
eos
|
86
|
+
end
|
87
|
+
|
88
|
+
def fill_templates
|
89
|
+
klass = create_klass
|
90
|
+
klass.send(:association_names=, model_definition.relationships)
|
91
|
+
klass.send(:attribute_names=, table_definition.column_names)
|
86
92
|
end
|
87
93
|
|
88
94
|
def add_relationships
|
89
95
|
klass = create_klass
|
90
96
|
model_definition.relationships.each do |m|
|
91
|
-
klass.
|
92
|
-
|
97
|
+
klass.send(:schema_attributes_template)[m] = nil
|
98
|
+
begin
|
99
|
+
klass.class_eval <<-eos, __FILE__, __LINE__+1
|
100
|
+
def #{m}
|
101
|
+
read_attribute(#{m.inspect})
|
102
|
+
end
|
103
|
+
|
104
|
+
def #{m}=(value)
|
105
|
+
write_attribute(#{m.inspect}, value)
|
106
|
+
end
|
107
|
+
eos
|
108
|
+
rescue SyntaxError
|
109
|
+
Logger_.debug "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
|
110
|
+
end
|
93
111
|
end
|
94
112
|
end
|
95
113
|
|
96
114
|
def add_method_mock_of
|
97
115
|
klass = create_klass
|
98
|
-
|
116
|
+
m_name = model_name
|
117
|
+
klass.instance_variable_set(:@model_class, model_definition.klass)
|
99
118
|
klass.instance_eval do
|
100
|
-
define_method(:mock_of) {
|
119
|
+
define_method(:mock_of) {m_name}
|
101
120
|
end
|
102
121
|
end
|
103
122
|
|
@@ -106,19 +125,43 @@ module ActiveMocker
|
|
106
125
|
table_definition.column_names.each do |m|
|
107
126
|
|
108
127
|
klass.send(:schema_attributes_template)[m] = nil
|
128
|
+
begin
|
129
|
+
klass.class_eval <<-eos, __FILE__, __LINE__+1
|
130
|
+
def #{m}
|
131
|
+
read_attribute(#{m.inspect})
|
132
|
+
end
|
133
|
+
|
134
|
+
def #{m}=(value)
|
135
|
+
write_attribute(#{m.inspect}, value)
|
136
|
+
end
|
137
|
+
eos
|
138
|
+
rescue SyntaxError
|
139
|
+
Logger_.debug "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
109
143
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
144
|
+
def add_relationships_methods
|
145
|
+
klass = create_klass
|
146
|
+
model_definition.relationships.each do |m|
|
147
|
+
klass.send(:schema_attributes_template)[m] = nil
|
148
|
+
begin
|
149
|
+
klass.class_eval <<-eos, __FILE__, __LINE__+1
|
150
|
+
def #{m}
|
151
|
+
read_attribute(#{m.inspect})
|
152
|
+
end
|
153
|
+
|
154
|
+
def #{m}=(value)
|
155
|
+
write_attribute(#{m.inspect}, value)
|
156
|
+
end
|
157
|
+
eos
|
158
|
+
rescue SyntaxError
|
159
|
+
Logger_.debug "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
|
160
|
+
end
|
119
161
|
end
|
120
162
|
end
|
121
163
|
|
164
|
+
|
122
165
|
def add_instance_methods
|
123
166
|
klass = create_klass
|
124
167
|
model_definition.instance_methods_with_arguments.each do |method|
|
@@ -130,7 +173,8 @@ module ActiveMocker
|
|
130
173
|
|
131
174
|
klass.class_eval <<-eos, __FILE__, __LINE__+1
|
132
175
|
def #{m}(#{params})
|
133
|
-
model_instance_methods[#{m.inspect}].
|
176
|
+
block = model_instance_methods[#{m.inspect}].to_proc
|
177
|
+
instance_exec(*[#{params_pass}], &block)
|
134
178
|
end
|
135
179
|
eos
|
136
180
|
end
|
@@ -142,19 +186,18 @@ module ActiveMocker
|
|
142
186
|
m = method.keys.first
|
143
187
|
params = Reparameterize.call(method.values.first)
|
144
188
|
params_pass = Reparameterize.call(method.values.first, true)
|
145
|
-
|
146
189
|
klass.send(:model_class_methods)[m] = eval_lambda(params, %Q[raise "::#{m} is not Implemented for Class: #{klass.name}"])
|
147
|
-
|
148
190
|
klass.class_eval <<-eos, __FILE__, __LINE__+1
|
149
191
|
def self.#{m}(#{params})
|
150
|
-
model_class_methods[#{m.inspect}].
|
192
|
+
block = model_class_methods[#{m.inspect}].to_proc
|
193
|
+
instance_exec(*[#{params_pass}], &block)
|
151
194
|
end
|
152
195
|
eos
|
153
196
|
end
|
154
197
|
end
|
155
198
|
|
156
199
|
def eval_lambda(arguments, block)
|
157
|
-
eval(%Q[ ->(#{arguments}){ #{block} }])
|
200
|
+
eval(%Q[ ->(#{arguments}){ #{block} }],binding, __FILE__, __LINE__)
|
158
201
|
end
|
159
202
|
|
160
203
|
def add_column_names_method
|
@@ -214,13 +257,22 @@ module ActiveMocker
|
|
214
257
|
|
215
258
|
private
|
216
259
|
|
260
|
+
def delegate_to_model_instance(method, *args)
|
261
|
+
self.class.send(:delegate_to_model_instance, method, *args)
|
262
|
+
end
|
263
|
+
|
264
|
+
def delegate_to_model_class(method, *args)
|
265
|
+
self.class.send(:delegate_to_model_class, method, *args)
|
266
|
+
end
|
267
|
+
|
217
268
|
def model_instance_methods
|
218
269
|
@model_instance_methods ||= self.class.send(:model_methods_template).dup
|
219
270
|
end
|
220
271
|
|
221
272
|
def schema_attributes
|
222
|
-
@schema_attributes ||= self.class.send(:
|
273
|
+
@schema_attributes ||= self.class.send(:attribute_template).dup
|
223
274
|
end
|
275
|
+
|
224
276
|
end
|
225
277
|
|
226
278
|
module ModelClassMethods
|
@@ -239,6 +291,14 @@ module ActiveMocker
|
|
239
291
|
|
240
292
|
private
|
241
293
|
|
294
|
+
def delegate_to_model_instance(method, *args)
|
295
|
+
model_class_instance.send(method, *args)
|
296
|
+
end
|
297
|
+
|
298
|
+
def delegate_to_model_class(method, *args)
|
299
|
+
model_class.send(method, *args)
|
300
|
+
end
|
301
|
+
|
242
302
|
def model_class_methods
|
243
303
|
@model_class_methods ||= HashWithIndifferentAccess.new
|
244
304
|
end
|
@@ -251,6 +311,44 @@ module ActiveMocker
|
|
251
311
|
@schema_attributes_template ||= HashWithIndifferentAccess.new
|
252
312
|
end
|
253
313
|
|
314
|
+
def model_class
|
315
|
+
@model_class
|
316
|
+
end
|
317
|
+
|
318
|
+
def model_class_instance
|
319
|
+
@model_class_instance ||= model_class.new
|
320
|
+
end
|
321
|
+
|
322
|
+
def attribute_names
|
323
|
+
@attribute_names
|
324
|
+
end
|
325
|
+
|
326
|
+
def attribute_names=(attributes)
|
327
|
+
@attribute_names = attributes.map{|a| a.to_sym}
|
328
|
+
end
|
329
|
+
|
330
|
+
def attribute_template
|
331
|
+
return @attribute_template unless @attribute_template.nil?
|
332
|
+
@attribute_template = HashWithIndifferentAccess.new
|
333
|
+
attribute_names.each {|a| @attribute_template[a] = nil}
|
334
|
+
return @attribute_template
|
335
|
+
end
|
336
|
+
|
337
|
+
def association_names
|
338
|
+
@association_names
|
339
|
+
end
|
340
|
+
|
341
|
+
def association_names=(associations)
|
342
|
+
@association_names = associations.map{|a| a.to_sym}
|
343
|
+
end
|
344
|
+
|
345
|
+
def association_template
|
346
|
+
return @association_template unless @association_template.nil?
|
347
|
+
@association_template = HashWithIndifferentAccess.new
|
348
|
+
association_names.each {|a| @association_template[a] = nil}
|
349
|
+
return @association_template
|
350
|
+
end
|
351
|
+
|
254
352
|
end
|
255
353
|
|
256
354
|
end
|
@@ -1,8 +1,10 @@
|
|
1
1
|
require 'rspec'
|
2
2
|
$:.unshift File.expand_path('../../', __FILE__)
|
3
|
+
require 'singleton'
|
3
4
|
require 'logger'
|
4
5
|
require 'active_mocker/logger'
|
5
6
|
require 'string_reader'
|
7
|
+
require 'active_mocker/public_methods'
|
6
8
|
require 'active_mocker/table'
|
7
9
|
require 'active_mocker/config'
|
8
10
|
require 'active_mocker/reparameterize'
|
@@ -18,7 +20,7 @@ require 'active_hash/ar_api'
|
|
18
20
|
describe ActiveMocker::Base do
|
19
21
|
|
20
22
|
before(:each) do
|
21
|
-
ActiveMocker
|
23
|
+
ActiveMocker.configure do |config|
|
22
24
|
# Required Options
|
23
25
|
config.schema_file = 'file is being inject as string'
|
24
26
|
config.model_dir = 'file is being inject as string'
|
@@ -38,7 +40,7 @@ describe ActiveMocker::Base do
|
|
38
40
|
end
|
39
41
|
|
40
42
|
let(:mock_class){
|
41
|
-
|
43
|
+
ActiveMocker.mock('Person')
|
42
44
|
}
|
43
45
|
|
44
46
|
after(:each) do
|
@@ -72,7 +74,7 @@ describe ActiveMocker::Base do
|
|
72
74
|
describe '::column_names' do
|
73
75
|
|
74
76
|
it 'returns an array of column names found from the schema.rb file' do
|
75
|
-
expect(mock_class.column_names).to eq(["account_id", "first_name", "last_name", "address", "city", "800_number"])
|
77
|
+
expect(mock_class.column_names).to eq(["id", "account_id", "first_name", "last_name", "address", "city", "800_number"])
|
76
78
|
end
|
77
79
|
|
78
80
|
end
|
@@ -104,18 +106,6 @@ describe ActiveMocker::Base do
|
|
104
106
|
|
105
107
|
end
|
106
108
|
|
107
|
-
context 'set to false' do
|
108
|
-
|
109
|
-
it 'will fail' do
|
110
|
-
described_class.mass_assignment = false
|
111
|
-
person = described_class.mock("Person")
|
112
|
-
expect{
|
113
|
-
person.new(first_name: "Sam", last_name: 'Walton')
|
114
|
-
}.to raise_error ArgumentError
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
109
|
end
|
120
110
|
|
121
111
|
describe '#mock_class' do
|
@@ -208,6 +198,9 @@ describe ActiveMocker::Base do
|
|
208
198
|
class Person < ActiveRecord::Base
|
209
199
|
def bar(name, type=nil)
|
210
200
|
end
|
201
|
+
|
202
|
+
def baz
|
203
|
+
end
|
211
204
|
end
|
212
205
|
eos
|
213
206
|
}
|
@@ -230,6 +223,65 @@ describe ActiveMocker::Base do
|
|
230
223
|
|
231
224
|
end
|
232
225
|
|
226
|
+
it 'can reference another mock' do
|
227
|
+
|
228
|
+
mock_class.mock_instance_method(:bar) do |name, type=nil|
|
229
|
+
"Now implemented with #{name} and #{type}"
|
230
|
+
end
|
231
|
+
|
232
|
+
mock_class.mock_instance_method(:baz) do
|
233
|
+
bar("name", 'type')
|
234
|
+
end
|
235
|
+
|
236
|
+
expect(mock_class.new.bar("name", 'type')).to eq "Now implemented with name and type"
|
237
|
+
expect(mock_class.new.baz).to eq "Now implemented with name and type"
|
238
|
+
end
|
239
|
+
|
240
|
+
context 'can call real code by delegating to model' do
|
241
|
+
|
242
|
+
let(:model_file){
|
243
|
+
StringReader.new <<-eos
|
244
|
+
class Person < ActiveRecord::Base
|
245
|
+
def bar(name, type=nil)
|
246
|
+
name + ' bar' + foo + ' ' +type
|
247
|
+
end
|
248
|
+
|
249
|
+
def foo
|
250
|
+
'foo'
|
251
|
+
end
|
252
|
+
|
253
|
+
def baz
|
254
|
+
end
|
255
|
+
|
256
|
+
def self.foobar
|
257
|
+
'foobar'
|
258
|
+
end
|
259
|
+
end
|
260
|
+
eos
|
261
|
+
}
|
262
|
+
|
263
|
+
it 'can delegate instance method to models instance method' do
|
264
|
+
|
265
|
+
mock_class.mock_instance_method(:bar) do |name, type=nil|
|
266
|
+
delegate_to_model_instance(:bar, name, type)
|
267
|
+
end
|
268
|
+
|
269
|
+
expect(mock_class.new.bar('name','type')).to eq "name barfoo type"
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'can delegate class method to models class method' do
|
274
|
+
|
275
|
+
mock_class.mock_class_method(:foobar) do
|
276
|
+
delegate_to_model_class(:foobar)
|
277
|
+
end
|
278
|
+
|
279
|
+
expect(mock_class.foobar).to eq "foobar"
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
233
285
|
end
|
234
286
|
|
235
287
|
describe 'class methods' do
|
@@ -311,7 +363,7 @@ describe ActiveMocker::Base do
|
|
311
363
|
end
|
312
364
|
|
313
365
|
it '::column_names' do
|
314
|
-
expect(mock_class.column_names).to eq(["account_id", "first_name", "last_name", "address", "city","800_number"])
|
366
|
+
expect(mock_class.column_names).to eq(["id", "account_id", "first_name", "last_name", "address", "city","800_number"])
|
315
367
|
end
|
316
368
|
|
317
369
|
it '#mock_of' do
|
@@ -64,7 +64,7 @@ describe ActiveMocker::SchemaReader do
|
|
64
64
|
it 'let not read a file but return a string instead to be evaluated' do
|
65
65
|
people = subject.search('people')
|
66
66
|
expect(people.name).to eq 'people'
|
67
|
-
expect(people.fields[
|
67
|
+
expect(people.fields[2].to_h).to eq({:name=>"first_name", :type=>:string, :options=>[{:limit=>128}]})
|
68
68
|
expect(subject.search('zip_codes').name).to eq 'zip_codes'
|
69
69
|
end
|
70
70
|
|
@@ -89,7 +89,7 @@ describe ActiveMocker::SchemaReader do
|
|
89
89
|
|
90
90
|
it 'returns an array of columns from the schema.rb' do
|
91
91
|
expect(people_search.name).to eq 'people'
|
92
|
-
expect(people_search.column_names).to eq ["company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]
|
92
|
+
expect(people_search.column_names).to eq ["id", "company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]
|
93
93
|
end
|
94
94
|
|
95
95
|
end
|
@@ -97,7 +97,7 @@ describe ActiveMocker::SchemaReader do
|
|
97
97
|
describe '#fields' do
|
98
98
|
|
99
99
|
it 'returns all fields from schema' do
|
100
|
-
expect(people_search.fields.
|
100
|
+
expect(people_search.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>[]})
|
101
101
|
end
|
102
102
|
|
103
103
|
end
|
@@ -7,20 +7,12 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
7
7
|
|
8
8
|
before(:each) do
|
9
9
|
ActiveMocker::Base.configure do |config|
|
10
|
-
# Required Options
|
11
10
|
config.schema_file = project_root + '/lib/active_record/db/schema.rb'
|
12
11
|
config.model_dir = project_root + '/lib/active_record/app/models'
|
13
|
-
|
14
|
-
|
15
|
-
config.
|
16
|
-
config.
|
17
|
-
config.active_hash_as_base = true #default
|
18
|
-
#config.schema_attributes = true #default
|
19
|
-
#config.model_relationships = true #default
|
20
|
-
#config.model_methods = true #default
|
21
|
-
#config.mass_assignment = true #default
|
22
|
-
# Logging
|
23
|
-
config.log_level = Logger::WARN #default
|
12
|
+
config.schema_file_reader = nil
|
13
|
+
config.model_file_reader = nil
|
14
|
+
config.active_hash_as_base = true
|
15
|
+
config.log_level = Logger::WARN
|
24
16
|
end
|
25
17
|
ActiveMocker::Base.mock('Person')
|
26
18
|
end
|
@@ -63,30 +55,10 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
63
55
|
let(:person_ar){Person.new(attributes)}
|
64
56
|
let(:person_mock){PersonMock.new(attributes)}
|
65
57
|
|
66
|
-
|
67
|
-
|
68
|
-
# Implementation Fix: On init give all values nil
|
69
|
-
|
70
|
-
it 'the mock will exclude any attributes with nil and have a symbol and string version' do
|
71
|
-
expect(person_mock.attributes).to eq({:first_name=>"Dustin", :last_name=>"Zeisler", "first_name"=>"Dustin", "last_name"=>"Zeisler"})
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'can still ask for attribute that is nil on mock' do
|
75
|
-
expect(person_mock[:middle_name]).to eq(nil)
|
58
|
+
it 'they are the same' do
|
59
|
+
expect(person_mock.attributes).to eq person_ar.attributes
|
76
60
|
end
|
77
61
|
|
78
|
-
it 'ar will include values with nil' do
|
79
|
-
expect(person_ar.attributes).to eq({"id"=>nil, "company_id"=>nil, "first_name"=>"Dustin", "middle_name"=>nil, "last_name"=>"Zeisler", "address_1"=>nil, "address_2"=>nil, "city"=>nil, "state_id"=>nil, "zip_code_id"=>nil, "title"=>nil, "department"=>nil, "person_email"=>nil, "work_phone"=>nil, "cell_phone"=>nil, "home_phone"=>nil, "fax"=>nil, "user_id_assistant"=>nil, "birth_date"=>nil, "needs_review"=>nil, "created_at"=>nil, "updated_at"=>nil})
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'compare access to attribute' do
|
83
|
-
expect(person_mock.first_name).to eq person_ar.first_name
|
84
|
-
expect(person_mock[:first_name]).to eq person_ar.first_name
|
85
|
-
expect(person_mock['first_name']).to eq person_ar.first_name
|
86
|
-
expect(person_ar[:first_name]).to eq person_mock.first_name
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
62
|
end
|
91
63
|
|
92
64
|
describe 'associations' do
|
@@ -97,12 +69,9 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
97
69
|
let(:person_ar){Person.new(create_attributes)}
|
98
70
|
let(:person_mock){PersonMock.new(create_attributes)}
|
99
71
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
it 'The Mock will include associations in attributes' do
|
105
|
-
expect(person_mock.attributes).to eq({:first_name=>"Dustin", :last_name=>"Zeisler", zip_code: zip_code, "first_name"=>"Dustin", "last_name"=>"Zeisler"})
|
72
|
+
it 'the Mock when adding an association will not set the _id attribute, do it manually' do
|
73
|
+
expect(person_mock.attributes).to eq({"id"=>nil, "company_id"=>nil, "first_name"=>"Dustin", "middle_name"=>nil, "last_name"=>"Zeisler", "address_1"=>nil, "address_2"=>nil, "city"=>nil, "state_id"=>nil, "zip_code_id"=>nil, "title"=>nil, "department"=>nil, "person_email"=>nil, "work_phone"=>nil, "cell_phone"=>nil, "home_phone"=>nil, "fax"=>nil, "user_id_assistant"=>nil, "birth_date"=>nil, "needs_review"=>nil, "created_at"=>nil, "updated_at"=>nil})
|
74
|
+
expect(person_mock.zip_code).to eq zip_code
|
106
75
|
end
|
107
76
|
|
108
77
|
it 'Ar will not include associations in attributes' do
|
@@ -115,12 +84,8 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
115
84
|
|
116
85
|
let(:column_names){["company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]}
|
117
86
|
|
118
|
-
it '
|
119
|
-
expect(PersonMock.column_names).to eq column_names
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'AR does include id column' do
|
123
|
-
expect(Person.column_names).to eq column_names.unshift('id')
|
87
|
+
it 'they are the same' do
|
88
|
+
expect(PersonMock.column_names).to eq Person.column_names
|
124
89
|
end
|
125
90
|
|
126
91
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_mocker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -160,6 +160,7 @@ extensions: []
|
|
160
160
|
extra_rdoc_files: []
|
161
161
|
files:
|
162
162
|
- ".gitignore"
|
163
|
+
- ".hound.yml"
|
163
164
|
- ".travis.yml"
|
164
165
|
- Gemfile
|
165
166
|
- LICENSE.txt
|
@@ -169,6 +170,7 @@ files:
|
|
169
170
|
- lib/active_hash/ar_api.rb
|
170
171
|
- lib/active_hash/destroy_all.rb
|
171
172
|
- lib/active_hash/find_by.rb
|
173
|
+
- lib/active_hash/init.rb
|
172
174
|
- lib/active_hash/update.rb
|
173
175
|
- lib/active_mocker.rb
|
174
176
|
- lib/active_mocker/active_record.rb
|
@@ -184,6 +186,7 @@ files:
|
|
184
186
|
- lib/active_mocker/field.rb
|
185
187
|
- lib/active_mocker/logger.rb
|
186
188
|
- lib/active_mocker/model_reader.rb
|
189
|
+
- lib/active_mocker/public_methods.rb
|
187
190
|
- lib/active_mocker/reparameterize.rb
|
188
191
|
- lib/active_mocker/schema_reader.rb
|
189
192
|
- lib/active_mocker/table.rb
|
@@ -236,3 +239,4 @@ test_files:
|
|
236
239
|
- spec/lib/model.rb
|
237
240
|
- spec/lib/person.rb
|
238
241
|
- spec/lib/schema.rb
|
242
|
+
has_rdoc:
|