active_mocker 1.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +61 -25
- data/Rakefile +1 -1
- data/active_mocker.gemspec +3 -3
- data/lib/active_hash/init.rb +13 -22
- data/lib/active_mocker.rb +1 -0
- data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
- data/lib/active_mocker/active_record/unknown_module.rb +3 -3
- data/lib/active_mocker/collection_association.rb +19 -20
- data/lib/active_mocker/config.rb +7 -4
- data/lib/active_mocker/field.rb +4 -4
- data/lib/active_mocker/generate.rb +75 -18
- data/lib/active_mocker/logger.rb +10 -1
- data/lib/active_mocker/mock_class_methods.rb +12 -2
- data/lib/active_mocker/mock_instance_methods.rb +3 -2
- data/lib/active_mocker/mock_requires.rb +2 -1
- data/lib/active_mocker/mock_template.erb +25 -22
- data/lib/active_mocker/public_methods.rb +6 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/app/models/micropost.rb +0 -2
- data/sample_app_rails_4/bin/rspec +16 -0
- data/sample_app_rails_4/config/application.rb +0 -3
- data/sample_app_rails_4/config/database.yml +5 -6
- data/sample_app_rails_4/config/environments/development.rb +0 -2
- data/sample_app_rails_4/config/environments/test.rb +0 -1
- data/sample_app_rails_4/config/initializers/active_mocker.rb +7 -5
- data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +1 -1
- data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +1 -1
- data/sample_app_rails_4/db/schema.rb +4 -3
- data/sample_app_rails_4/lib/tasks/{mocks.rake → active_mocker.rake} +5 -5
- data/sample_app_rails_4/lib/unit_logger.rb +22 -0
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +110 -4
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +31 -27
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +29 -24
- data/sample_app_rails_4/spec/mocks/user_mock.rb +69 -58
- data/sample_app_rails_4/spec/spec_helper.rb +6 -7
- data/sample_app_rails_4/spec/user_mock_spec.rb +14 -7
- data/spec/lib/active_mocker/collection_association_spec.rb +17 -3
- data/spec/lib/active_mocker/generate_spec.rb +8 -6
- data/spec/lib/active_mocker/model_reader_spec.rb +5 -0
- data/spec/lib/active_mocker/schema_reader_spec.rb +1 -1
- data/spec/lib/readme_spec.rb +199 -205
- data/spec/unit_logger.rb +24 -0
- metadata +22 -32
- data/mocks/micropost_mock.rb +0 -108
- data/mocks/relationship_mock.rb +0 -109
- data/mocks/user_mock.rb +0 -199
- data/plan_mock.rb +0 -2323
- data/sample_app_rails_4/config/cucumber.yml +0 -8
- data/sample_app_rails_4/db/development.sqlite3 +0 -0
- data/sample_app_rails_4/db/test.sqlite3 +0 -0
- data/spec/lib/active_mocker/performance/base_spec.rb +0 -454
- data/spec/lib/active_mocker/performance/large_schema.rb +0 -3576
- data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
- data/spec/lib/active_mocker/performance/schema_reader_spec.rb +0 -96
- data/spec/lib/compare_mocker_and_record_spec.rb +0 -133
@@ -1,8 +0,0 @@
|
|
1
|
-
<%
|
2
|
-
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
|
3
|
-
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
|
4
|
-
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
|
5
|
-
%>
|
6
|
-
default: <%= std_opts %> features
|
7
|
-
wip: --tags @wip:3 --wip features
|
8
|
-
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
|
Binary file
|
Binary file
|
@@ -1,454 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
$:.unshift File.expand_path('../../', __FILE__)
|
3
|
-
require 'forwardable'
|
4
|
-
require 'singleton'
|
5
|
-
require 'logger'
|
6
|
-
require 'active_mocker/logger'
|
7
|
-
require 'string_reader'
|
8
|
-
require 'file_reader'
|
9
|
-
require 'active_mocker/public_methods'
|
10
|
-
require 'active_mocker/collection_association'
|
11
|
-
require 'active_mocker/table'
|
12
|
-
require 'active_mocker/config'
|
13
|
-
require 'active_mocker/reparameterize'
|
14
|
-
require 'active_mocker/field'
|
15
|
-
require 'active_mocker/active_record'
|
16
|
-
require 'active_mocker/model_reader'
|
17
|
-
require 'active_mocker/schema_reader'
|
18
|
-
require 'active_mocker/generate'
|
19
|
-
require 'active_mocker/active_record/schema'
|
20
|
-
require 'active_support/all'
|
21
|
-
require 'active_hash/ar_api'
|
22
|
-
require 'erb'
|
23
|
-
describe ActiveMocker, pending: true do
|
24
|
-
|
25
|
-
before(:each) do
|
26
|
-
ActiveMocker.configure do |config|
|
27
|
-
# Required Options
|
28
|
-
config.schema_file = File.join(File.expand_path('../../', __FILE__), 'performance/large_schema.rb')
|
29
|
-
config.model_dir = 'file is being inject as string'
|
30
|
-
# Dependency injection
|
31
|
-
config.model_file_reader = model_file
|
32
|
-
# Additional Options
|
33
|
-
config.schema_attributes = true #default
|
34
|
-
config.model_attributes = true #default
|
35
|
-
config.clear_cache = true #default
|
36
|
-
config.migration_dir = '/Users/zeisler/dev/active_mocker/spec/lib/active_mocker/performance/migration'
|
37
|
-
# Logging
|
38
|
-
config.log_level = Logger::WARN #default
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
let(:mock_class){
|
44
|
-
ActiveMocker.mock('Person')
|
45
|
-
}
|
46
|
-
|
47
|
-
let(:model_file){
|
48
|
-
StringReader.new <<-eos
|
49
|
-
class Plan < ActiveRecord::Base
|
50
|
-
|
51
|
-
def for_real(abc)
|
52
|
-
puts abc
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
eos
|
57
|
-
}
|
58
|
-
|
59
|
-
|
60
|
-
describe '::column_names' do
|
61
|
-
|
62
|
-
it 'returns an array of column names found from the schema.rb file' do
|
63
|
-
expect(mock_class.column_names).to eq(["id", "account_id", "first_name", "last_name", "address", "city", "800_number"])
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
describe 'mass_assignment' do
|
69
|
-
|
70
|
-
it "can pass any or all attributes from schema in initializer" do
|
71
|
-
result = mock_class.new(first_name: "Sam", last_name: 'Walton')
|
72
|
-
expect(result.first_name).to eq 'Sam'
|
73
|
-
expect(result.last_name).to eq 'Walton'
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'will raise error if not an attribute or association' do
|
78
|
-
expect{mock_class.new(baz: "Hello")}.to raise_error('Rejected params: {"baz"=>"Hello"} for PersonMock')
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
describe '#mock_class' do
|
84
|
-
|
85
|
-
it 'create a mock object after the active record' do
|
86
|
-
expect(mock_class).to eq(PersonMock)
|
87
|
-
end
|
88
|
-
|
89
|
-
context 'private methods' do
|
90
|
-
|
91
|
-
let(:model_file){
|
92
|
-
StringReader.new <<-eos
|
93
|
-
class Person < ActiveRecord::Base
|
94
|
-
private
|
95
|
-
|
96
|
-
def bar
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|
100
|
-
eos
|
101
|
-
}
|
102
|
-
|
103
|
-
it 'will not have private methods' do
|
104
|
-
expect{mock_class.bar}.to raise_error(NoMethodError)
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
describe 'relationships' do
|
110
|
-
|
111
|
-
let(:model_file){
|
112
|
-
StringReader.new <<-eos
|
113
|
-
class Person < ActiveRecord::Base
|
114
|
-
belongs_to :account
|
115
|
-
has_many :files
|
116
|
-
end
|
117
|
-
eos
|
118
|
-
}
|
119
|
-
|
120
|
-
it 'add instance methods from model relationships' do
|
121
|
-
result = mock_class.new(account: 'Account')
|
122
|
-
expect(result.account).to eq 'Account'
|
123
|
-
end
|
124
|
-
|
125
|
-
it 'add has_many relationship' do
|
126
|
-
|
127
|
-
expect(mock_class.new.files.class).to eq ActiveMocker::CollectionAssociation
|
128
|
-
expect(mock_class.new.files.count).to eq 0
|
129
|
-
mock_inst = mock_class.new
|
130
|
-
mock_inst.files << 1
|
131
|
-
expect(mock_inst.files.count).to eq 1
|
132
|
-
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
describe 'conflict of instance mocks and class mocks' do
|
138
|
-
|
139
|
-
let(:model_file){
|
140
|
-
StringReader.new <<-eos
|
141
|
-
class Person < ActiveRecord::Base
|
142
|
-
def bar(name, type=nil)
|
143
|
-
end
|
144
|
-
|
145
|
-
def self.bar
|
146
|
-
end
|
147
|
-
end
|
148
|
-
eos
|
149
|
-
}
|
150
|
-
|
151
|
-
it 'can mock instance method and class method of the same name' do
|
152
|
-
result = mock_class.new
|
153
|
-
result.mock_instance_method(:bar) do |name, type=nil|
|
154
|
-
"Now implemented with #{name} and #{type}"
|
155
|
-
end
|
156
|
-
expect{result.bar}.to raise_error ArgumentError
|
157
|
-
result = result.bar('foo', 'type')
|
158
|
-
expect(result).to eq "Now implemented with foo and type"
|
159
|
-
expect{mock_class.bar}.to raise_error(RuntimeError, '::bar is not Implemented for Class: PersonMock')
|
160
|
-
|
161
|
-
mock_class.mock_class_method(:bar) do
|
162
|
-
"Now implemented"
|
163
|
-
end
|
164
|
-
expect{mock_class.bar}.to_not raise_error
|
165
|
-
expect(mock_class.bar).to eq("Now implemented")
|
166
|
-
end
|
167
|
-
|
168
|
-
end
|
169
|
-
|
170
|
-
describe 'instance methods' do
|
171
|
-
|
172
|
-
let(:model_file){
|
173
|
-
StringReader.new <<-eos
|
174
|
-
class Plan < ActiveRecord::Base
|
175
|
-
def bar(name, type=nil)
|
176
|
-
end
|
177
|
-
|
178
|
-
def baz
|
179
|
-
end
|
180
|
-
end
|
181
|
-
eos
|
182
|
-
}
|
183
|
-
|
184
|
-
it 'will raise exception for unimplemented methods' do
|
185
|
-
expect(mock_class.new.method(:bar).parameters).to eq [[:req, :name], [:opt, :type]]
|
186
|
-
expect{mock_class.new.bar}.to raise_error ArgumentError
|
187
|
-
expect{mock_class.new.bar('foo', 'type')}.to raise_error('#bar is not Implemented for Class: PersonMock')
|
188
|
-
end
|
189
|
-
|
190
|
-
it 'can be implemented dynamically' do
|
191
|
-
|
192
|
-
mock_class.mock_instance_method(:bar) do |name, type=nil|
|
193
|
-
"Now implemented with #{name} and #{type}"
|
194
|
-
end
|
195
|
-
|
196
|
-
result = mock_class.new
|
197
|
-
result = result.bar('foo', 'type')
|
198
|
-
expect(result).to eq "Now implemented with foo and type"
|
199
|
-
|
200
|
-
end
|
201
|
-
|
202
|
-
it 'can reference another mock' do
|
203
|
-
|
204
|
-
mock_class.mock_instance_method(:bar) do |name, type=nil|
|
205
|
-
"Now implemented with #{name} and #{type}"
|
206
|
-
end
|
207
|
-
|
208
|
-
mock_class.mock_instance_method(:baz) do
|
209
|
-
bar("name", 'type')
|
210
|
-
end
|
211
|
-
|
212
|
-
expect(mock_class.new.bar("name", 'type')).to eq "Now implemented with name and type"
|
213
|
-
expect(mock_class.new.baz).to eq "Now implemented with name and type"
|
214
|
-
end
|
215
|
-
|
216
|
-
context 'can call real code by delegating to model' do
|
217
|
-
|
218
|
-
let(:model_file){
|
219
|
-
StringReader.new <<-eos
|
220
|
-
class Person < ActiveRecord::Base
|
221
|
-
def bar(name, type=nil)
|
222
|
-
name + ' bar' + foo + ' ' +type
|
223
|
-
end
|
224
|
-
|
225
|
-
def foo
|
226
|
-
'foo'
|
227
|
-
end
|
228
|
-
|
229
|
-
def baz
|
230
|
-
end
|
231
|
-
|
232
|
-
def self.foobar
|
233
|
-
'foobar'
|
234
|
-
end
|
235
|
-
end
|
236
|
-
eos
|
237
|
-
}
|
238
|
-
|
239
|
-
it 'can delegate instance method to models instance method' do
|
240
|
-
|
241
|
-
mock_class.mock_instance_method(:bar) do |name, type=nil|
|
242
|
-
delegate_to_model_instance(:bar, name, type)
|
243
|
-
end
|
244
|
-
|
245
|
-
expect(mock_class.new.bar('name','type')).to eq "name barfoo type"
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
it 'can delegate class method to models class method' do
|
250
|
-
|
251
|
-
mock_class.mock_class_method(:foobar) do
|
252
|
-
delegate_to_model_class(:foobar)
|
253
|
-
end
|
254
|
-
|
255
|
-
expect(mock_class.foobar).to eq "foobar"
|
256
|
-
|
257
|
-
end
|
258
|
-
|
259
|
-
end
|
260
|
-
|
261
|
-
end
|
262
|
-
|
263
|
-
describe 'class methods' do
|
264
|
-
|
265
|
-
let(:model_file){
|
266
|
-
StringReader.new <<-eos
|
267
|
-
class Person < ActiveRecord::Base
|
268
|
-
scope :named, -> { }
|
269
|
-
|
270
|
-
def self.class_method
|
271
|
-
end
|
272
|
-
end
|
273
|
-
eos
|
274
|
-
}
|
275
|
-
|
276
|
-
it 'will raise exception for unimplemented methods' do
|
277
|
-
expect{mock_class.class_method}.to raise_error('::class_method is not Implemented for Class: PersonMock')
|
278
|
-
end
|
279
|
-
|
280
|
-
it 'can be implemented as follows' do
|
281
|
-
|
282
|
-
mock_class.mock_class_method(:class_method) do
|
283
|
-
"Now implemented"
|
284
|
-
end
|
285
|
-
expect{mock_class.class_method}.to_not raise_error
|
286
|
-
expect(mock_class.class_method).to eq("Now implemented")
|
287
|
-
|
288
|
-
end
|
289
|
-
|
290
|
-
it 'loads named scopes as class method' do
|
291
|
-
expect{mock_class.named}.to raise_error('::named is not Implemented for Class: PersonMock')
|
292
|
-
end
|
293
|
-
|
294
|
-
end
|
295
|
-
|
296
|
-
end
|
297
|
-
|
298
|
-
context 'active_hash' do
|
299
|
-
|
300
|
-
let(:model_file){
|
301
|
-
StringReader.new <<-eos
|
302
|
-
class Person < ActiveRecord::Base
|
303
|
-
belongs_to :account
|
304
|
-
|
305
|
-
def bar
|
306
|
-
end
|
307
|
-
|
308
|
-
end
|
309
|
-
eos
|
310
|
-
}
|
311
|
-
|
312
|
-
it 'uses active_hash::base as superclass' do
|
313
|
-
expect(mock_class.superclass.name).to eq 'ActiveHash::Base'
|
314
|
-
end
|
315
|
-
|
316
|
-
it 'can mass assign attributes to constructor' do
|
317
|
-
result = mock_class.new(first_name: "Sam", last_name: 'Walton', account: 0)
|
318
|
-
expect(result.first_name).to eq 'Sam'
|
319
|
-
expect(result.last_name).to eq 'Walton'
|
320
|
-
expect(result.account).to eq 0
|
321
|
-
end
|
322
|
-
|
323
|
-
it 'can save to class and then find instance by attribute' do
|
324
|
-
|
325
|
-
record = mock_class.create(first_name: "Sam", last_name: 'Walton')
|
326
|
-
expect(mock_class.find_by(first_name:"Sam")).to eq record
|
327
|
-
|
328
|
-
end
|
329
|
-
|
330
|
-
it '::column_names' do
|
331
|
-
expect(mock_class.column_names).to eq(["id", "account_id", "first_name", "last_name", "address", "city","800_number"])
|
332
|
-
end
|
333
|
-
|
334
|
-
it 'instance methods from model' do
|
335
|
-
expect{mock_class.new.bar}.to raise_error '#bar is not Implemented for Class: PersonMock'
|
336
|
-
end
|
337
|
-
|
338
|
-
let(:model_file){
|
339
|
-
StringReader.new <<-eos
|
340
|
-
class Person < ActiveRecord::Base
|
341
|
-
belongs_to :account
|
342
|
-
|
343
|
-
def bar
|
344
|
-
end
|
345
|
-
|
346
|
-
end
|
347
|
-
eos
|
348
|
-
}
|
349
|
-
|
350
|
-
it '#update' do
|
351
|
-
|
352
|
-
person = mock_class.create(first_name: 'Justin')
|
353
|
-
|
354
|
-
expect(PersonMock.first.first_name).to eq 'Justin'
|
355
|
-
person.update(first_name: 'Dustin')
|
356
|
-
expect(PersonMock.first.first_name).to eq 'Dustin'
|
357
|
-
|
358
|
-
expect(person.first_name).to eq 'Dustin'
|
359
|
-
|
360
|
-
|
361
|
-
end
|
362
|
-
|
363
|
-
it '::destroy_all' do
|
364
|
-
|
365
|
-
mock_class.create
|
366
|
-
|
367
|
-
expect(mock_class.count).to eq 1
|
368
|
-
|
369
|
-
mock_class.destroy_all
|
370
|
-
|
371
|
-
expect(mock_class.count).to eq 0
|
372
|
-
|
373
|
-
end
|
374
|
-
|
375
|
-
it '::find_by' do
|
376
|
-
person = mock_class.create(first_name: 'dustin')
|
377
|
-
expect(mock_class.find_by(first_name: 'dustin')).to eq person
|
378
|
-
end
|
379
|
-
|
380
|
-
it '::find_or_create_by' do
|
381
|
-
person = mock_class.find_or_create_by(first_name: 'dustin')
|
382
|
-
expect(mock_class.find_by(first_name: 'dustin')).to eq person
|
383
|
-
person = mock_class.find_or_create_by(first_name: 'dustin')
|
384
|
-
expect(mock_class.count).to eq 1
|
385
|
-
end
|
386
|
-
|
387
|
-
it '::find_or_create_by with update' do
|
388
|
-
mock_class.create(first_name: 'dustin')
|
389
|
-
person = mock_class.find_or_create_by(first_name: 'dustin')
|
390
|
-
person.update(last_name: 'Zeisler')
|
391
|
-
expect(mock_class.first.attributes).to eq person.attributes
|
392
|
-
expect(mock_class.count).to eq 1
|
393
|
-
end
|
394
|
-
|
395
|
-
it '::find_or_initialize_by' do
|
396
|
-
person = mock_class.find_or_initialize_by(first_name: 'dustin')
|
397
|
-
expect(person.persisted?).to eq false
|
398
|
-
mock_class.create(first_name: 'dustin')
|
399
|
-
person = mock_class.find_or_initialize_by(first_name: 'dustin')
|
400
|
-
expect(person.persisted?).to eq true
|
401
|
-
end
|
402
|
-
|
403
|
-
after(:each) do
|
404
|
-
mock_class.delete_all
|
405
|
-
end
|
406
|
-
|
407
|
-
|
408
|
-
end
|
409
|
-
|
410
|
-
describe '::configure' do
|
411
|
-
|
412
|
-
it 'requires schema_file' do
|
413
|
-
ActiveMocker::Base.reload_default
|
414
|
-
expect{
|
415
|
-
ActiveMocker::Base.configure {
|
416
|
-
}
|
417
|
-
}.to raise_error
|
418
|
-
|
419
|
-
end
|
420
|
-
|
421
|
-
|
422
|
-
it 'requires model_dir' do
|
423
|
-
ActiveMocker::Base.reload_default
|
424
|
-
expect{
|
425
|
-
ActiveMocker::Base.configure { |c|
|
426
|
-
c.schema_file = 'dir'
|
427
|
-
}
|
428
|
-
}.to raise_error
|
429
|
-
|
430
|
-
end
|
431
|
-
|
432
|
-
end
|
433
|
-
|
434
|
-
describe 'cannot redefine attributes method' do
|
435
|
-
|
436
|
-
let(:model_file){
|
437
|
-
StringReader.new <<-eos
|
438
|
-
class Person < ActiveRecord::Base
|
439
|
-
def attributes; end
|
440
|
-
|
441
|
-
end
|
442
|
-
eos
|
443
|
-
}
|
444
|
-
|
445
|
-
it 'still works' do
|
446
|
-
|
447
|
-
mock_class.new.attributes
|
448
|
-
|
449
|
-
end
|
450
|
-
|
451
|
-
|
452
|
-
end
|
453
|
-
|
454
|
-
end
|