active_mocker 1.3 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -11
- data/lib/active_hash/ar_api.rb +1 -1
- data/lib/active_mocker/collection/queries.rb +29 -18
- data/lib/active_mocker/mock_instance_methods.rb +2 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ca865ce8b72156b2e63b97bc17e7e3b46680812
|
4
|
+
data.tar.gz: 1c18749e4ecd00a17be85f50a7f7c474f3663d57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa3dbe4b14f5bff23610ac09c81390bc402e75dc1689b93b8a1500d48fdf80c65b3dfdd085edafcc86f38db1a6c535434dc3a6cac7d762b1f698f7e9dcd8196d
|
7
|
+
data.tar.gz: c0844f0b7d454cbc2a4d81965c8f7625cf428a3faaf66cf17fb77dca11a244b10ad15969f583c3fe4008fb5644700156b869b19a36cb23baa9c0be179823461e
|
data/README.md
CHANGED
@@ -165,22 +165,25 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
165
165
|
|
166
166
|
### ActiveRecord supported methods
|
167
167
|
**class methods**
|
168
|
-
|
168
|
+
|
169
169
|
* new
|
170
170
|
* create/create!
|
171
|
-
* column_names
|
171
|
+
* column_names/attribute_names
|
172
172
|
* find
|
173
173
|
* find_by/find_by!
|
174
174
|
* find_or_create_by
|
175
175
|
* find_or_initialize_by
|
176
176
|
* where(conditions_hash)
|
177
|
+
* where(key: array_of_values)
|
178
|
+
* where.not(conditions_hash)
|
177
179
|
* delete_all/destroy_all
|
178
180
|
* delete_all(conditions_hash)
|
179
181
|
* destroy(id)/delete(id)
|
182
|
+
* update_all
|
180
183
|
* all
|
181
184
|
* count
|
182
185
|
* first/last
|
183
|
-
|
186
|
+
* limit
|
184
187
|
|
185
188
|
**instance methods**
|
186
189
|
|
@@ -188,9 +191,10 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
188
191
|
* update
|
189
192
|
* save/save!
|
190
193
|
* write_attribute/read_attribute - (private, can be used within an included module)
|
194
|
+
* delete
|
195
|
+
|
196
|
+
**has_many associations/Collections**
|
191
197
|
|
192
|
-
**has_many associations**
|
193
|
-
|
194
198
|
* empty?
|
195
199
|
* length/size/count
|
196
200
|
* uniq
|
@@ -201,17 +205,31 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
201
205
|
* push
|
202
206
|
* clear
|
203
207
|
* take
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
208
|
+
* average(:field_name)
|
209
|
+
* minimum(:field_name)
|
210
|
+
* maximum(:field_name)
|
211
|
+
* sum(:field_name)
|
212
|
+
* find
|
213
|
+
* find_by/find_by!
|
214
|
+
* where(conditions_hash)
|
215
|
+
* where(key: array_of_values)
|
216
|
+
* where.not(conditions_hash)
|
217
|
+
* update_all
|
218
|
+
* delete_all
|
219
|
+
* order(:field_name)
|
220
|
+
* reverse_order
|
221
|
+
* limit
|
222
|
+
|
223
|
+
### Schema/Migration Option Support
|
224
|
+
* All schema types are supported and on initalization coerced by Virtus. If coercsion fails the passed value will be retained.
|
225
|
+
* Default value
|
209
226
|
|
210
227
|
### Known Limitations
|
211
|
-
|
212
228
|
* Model names and table names must follow the default ActiveRecord naming pattern.
|
213
229
|
* Included/extended module methods will not be included on the mock. I suggest you keep domain logic out of the model and only add database queries. Domain logic can be put into modules and then included into the mock during test setup.
|
214
230
|
* Queries will not call other mocks classes, for example when using `where` all attributes must reside inside of each record.
|
231
|
+
* Creation of association like `User.create_friend` or `User.build_friend` are not supported. If you need this functionality use rspec's stub any instance.
|
232
|
+
* Validation are not present in mocks.
|
215
233
|
|
216
234
|
## Inspiration
|
217
235
|
Thanks to Jeff Olfert for being my original inspiration for this project.
|
data/lib/active_hash/ar_api.rb
CHANGED
@@ -31,7 +31,7 @@ module ActiveMocker
|
|
31
31
|
include ActiveMocker::Collection::Queries
|
32
32
|
|
33
33
|
def create(attributes = {}, &block)
|
34
|
-
record = new(attributes)
|
34
|
+
record = new(attributes) unless block_given?
|
35
35
|
record = new(attributes, &block) if block_given?
|
36
36
|
record.save
|
37
37
|
mark_dirty
|
@@ -1,28 +1,24 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
|
3
|
-
|
3
|
+
class RecordNotFound < Exception; end
|
4
4
|
|
5
|
-
end
|
6
5
|
module Collection
|
7
6
|
|
8
|
-
|
9
|
-
|
10
7
|
module Queries
|
11
8
|
|
12
|
-
|
13
|
-
all.map(&:delete)
|
14
|
-
end
|
9
|
+
class Find
|
15
10
|
|
16
|
-
|
17
|
-
|
18
|
-
|
11
|
+
def initialize(record)
|
12
|
+
@record = record
|
13
|
+
end
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
15
|
+
def is_of(options={})
|
16
|
+
options.all? do |col, match|
|
17
|
+
next match.any? { |m| @record.send(col) == m } if match.class == Array
|
18
|
+
@record.send(col) == match
|
19
|
+
end
|
25
20
|
end
|
21
|
+
|
26
22
|
end
|
27
23
|
|
28
24
|
class WhereNotChain
|
@@ -33,16 +29,31 @@ module Collection
|
|
33
29
|
|
34
30
|
def not(options={})
|
35
31
|
@collection.reject do |record|
|
36
|
-
|
32
|
+
Find.new(record).is_of(options)
|
37
33
|
end
|
38
34
|
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete_all
|
38
|
+
all.map(&:delete)
|
39
|
+
end
|
40
|
+
|
41
|
+
def destroy_all
|
42
|
+
delete_all
|
43
|
+
end
|
39
44
|
|
45
|
+
def all(options={})
|
46
|
+
if options.has_key?(:conditions)
|
47
|
+
where(options[:conditions])
|
48
|
+
else
|
49
|
+
Relation.new( to_a || [] )
|
50
|
+
end
|
40
51
|
end
|
41
52
|
|
42
53
|
def where(options=nil)
|
43
54
|
return WhereNotChain.new(all) if options.nil?
|
44
55
|
all.select do |record|
|
45
|
-
|
56
|
+
Find.new(record).is_of(options)
|
46
57
|
end
|
47
58
|
end
|
48
59
|
|
@@ -52,7 +63,7 @@ module Collection
|
|
52
63
|
where(id: id).first
|
53
64
|
end
|
54
65
|
return Relation.new(results) if ids.class == Array
|
55
|
-
|
66
|
+
results.first
|
56
67
|
end
|
57
68
|
|
58
69
|
def update_all(options)
|
@@ -69,11 +69,11 @@ module MockInstanceMethods
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def model_instance_methods
|
72
|
-
@model_instance_methods ||= self.class.send(:model_instance_methods)
|
72
|
+
@model_instance_methods ||= self.class.send(:model_instance_methods)
|
73
73
|
end
|
74
74
|
|
75
75
|
def model_class_methods
|
76
|
-
@model_class_methods ||= self.class.send(:model_class_methods)
|
76
|
+
@model_class_methods ||= self.class.send(:model_class_methods)
|
77
77
|
end
|
78
78
|
|
79
79
|
def schema_attributes
|
@@ -261,6 +261,22 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
261
261
|
where_by_association(UserMock, MicropostMock)
|
262
262
|
end
|
263
263
|
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'passing array as value' do
|
267
|
+
|
268
|
+
def array_as_value(user_class)
|
269
|
+
users = [user_class.create!(email: '1', name: 'Alice'), user_class.create!(email: '2', name: 'Bob')]
|
270
|
+
expect(user_class.where({name: ["Alice", "Bob"]})).to eq(users)
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'User' do
|
274
|
+
array_as_value(User)
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'UserMock' do
|
278
|
+
array_as_value(UserMock)
|
279
|
+
end
|
264
280
|
|
265
281
|
end
|
266
282
|
|
@@ -830,4 +846,6 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
830
846
|
|
831
847
|
end
|
832
848
|
|
849
|
+
|
850
|
+
|
833
851
|
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:
|
4
|
+
version: 1.3.1
|
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-05-
|
11
|
+
date: 2014-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|