active_mocker 2.0.0.beta1 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -1
  3. data/README.md +11 -18
  4. data/lib/active_mocker.rb +5 -0
  5. data/lib/active_mocker/config.rb +7 -8
  6. data/lib/active_mocker/{mock → deprecated_components}/mock_abilities.rb +19 -7
  7. data/lib/active_mocker/deprecated_components/rspec.rb +12 -0
  8. data/lib/active_mocker/display_errors.rb +64 -0
  9. data/lib/active_mocker/error_object.rb +46 -0
  10. data/lib/active_mocker/generate.rb +30 -64
  11. data/lib/active_mocker/mock.rb +1 -2
  12. data/lib/active_mocker/mock/association.rb +0 -2
  13. data/lib/active_mocker/mock/base.rb +270 -256
  14. data/lib/active_mocker/mock/belongs_to.rb +14 -20
  15. data/lib/active_mocker/mock/collection.rb +0 -6
  16. data/lib/active_mocker/mock/do_nothing_active_record_methods.rb +39 -41
  17. data/lib/active_mocker/mock/exceptions.rb +4 -11
  18. data/lib/active_mocker/mock/has_and_belongs_to_many.rb +0 -2
  19. data/lib/active_mocker/mock/has_many.rb +4 -5
  20. data/lib/active_mocker/mock/has_one.rb +5 -11
  21. data/lib/active_mocker/mock/hash_process.rb +14 -17
  22. data/lib/active_mocker/mock/mock_relation.rb +10 -0
  23. data/lib/active_mocker/mock/object_inspect.rb +29 -32
  24. data/lib/active_mocker/mock/queries.rb +14 -18
  25. data/lib/active_mocker/mock/records.rb +45 -43
  26. data/lib/active_mocker/mock/relation.rb +1 -4
  27. data/lib/active_mocker/mock/single_relation.rb +13 -17
  28. data/lib/active_mocker/mock/template_methods.rb +1 -4
  29. data/lib/active_mocker/mock_creator.rb +4 -5
  30. data/lib/active_mocker/mock_template/_associations.erb +6 -6
  31. data/lib/active_mocker/mock_template/_class_methods.erb +1 -1
  32. data/lib/active_mocker/mock_template/_scopes.erb +3 -3
  33. data/lib/active_mocker/parent_class.rb +1 -1
  34. data/lib/active_mocker/public_methods.rb +5 -2
  35. data/lib/active_mocker/rspec.rb +0 -8
  36. data/lib/active_mocker/rspec_helper.rb +0 -2
  37. data/lib/active_mocker/task.rake +0 -2
  38. data/lib/active_mocker/version.rb +1 -1
  39. metadata +36 -19
  40. data/lib/active_mocker/logger.rb +0 -15
  41. data/lib/active_mocker/output_capture.rb +0 -32
@@ -1,62 +1,64 @@
1
1
  module ActiveMocker
2
- module Mock
3
- class Records
2
+ class Records
4
3
 
5
- extend Forwardable
6
- def_delegators :records, :<<, :count, :length, :to_a
4
+ extend Forwardable
5
+ def_delegators :records, :<<, :count, :length, :to_a
7
6
 
8
- attr_reader :records
9
- private :records
7
+ attr_reader :records
8
+ private :records
10
9
 
11
- def initialize(records = [])
12
- @records = records
13
- end
10
+ def initialize(records = [])
11
+ @records = records
12
+ end
14
13
 
15
- def insert(record)
16
- records << validate_unique_id((record.id ||= next_id), record)
17
- end
14
+ def insert(record)
15
+ records << validate_id((record.id ||= next_id), record)
16
+ end
18
17
 
19
- def delete(record)
20
- raise RecordNotFound, 'Record has not been created.' unless records.delete(record)
21
- end
18
+ def delete(record)
19
+ raise RecordNotFound, 'Record has not been created.' unless records.delete(record)
20
+ end
22
21
 
23
- def exists?(record)
24
- records.include?(record)
25
- end
22
+ def exists?(record)
23
+ records.include?(record)
24
+ end
26
25
 
27
- def new_record?(record)
28
- !exists?(record)
29
- end
26
+ def new_record?(record)
27
+ !exists?(record)
28
+ end
30
29
 
31
- def persisted?(id)
32
- ids.include?(id)
33
- end
30
+ def persisted?(id)
31
+ ids.include?(id)
32
+ end
34
33
 
35
- def reset
36
- records.clear
37
- end
34
+ def reset
35
+ records.clear
36
+ end
38
37
 
39
- private
38
+ private
40
39
 
41
- def ids
42
- records.map(&:id)
43
- end
40
+ def ids
41
+ records.map(&:id)
42
+ end
44
43
 
45
- def next_id
46
- max_record.succ
47
- rescue NoMethodError
48
- 1
49
- end
44
+ def next_id
45
+ max_record.succ
46
+ rescue NoMethodError
47
+ 1
48
+ end
50
49
 
51
- def max_record
52
- ids.max
53
- end
50
+ def max_record
51
+ ids.max
52
+ end
54
53
 
55
- def validate_unique_id(id, record)
56
- raise IdError, "Duplicate ID found for record #{record.inspect}" if persisted?(id)
57
- record
58
- end
54
+ def validate_id(id, record)
55
+ record.id = id.to_i
56
+ validate_unique_id(id, record)
57
+ end
59
58
 
59
+ def validate_unique_id(id, record)
60
+ raise IdError, "Duplicate ID found for record #{record.inspect}" if persisted?(id)
61
+ record
60
62
  end
61
63
  end
62
64
  end
@@ -1,7 +1,5 @@
1
1
  module ActiveMocker
2
- module Mock
3
2
  class Relation < Collection
4
-
5
3
  include Queries
6
4
 
7
5
  def initialize(collection=[])
@@ -10,7 +8,7 @@ module Mock
10
8
  end
11
9
 
12
10
  def inspect
13
- entries = to_a.take(11).map!(&:inspect)
11
+ entries = to_a.take(11).map!(&:inspect)
14
12
  entries[10] = '...' if entries.size == 11
15
13
  "#<#{self.class.name} [#{entries.join(', ')}]>"
16
14
  end
@@ -26,5 +24,4 @@ module Mock
26
24
  end
27
25
 
28
26
  end
29
- end
30
27
  end
@@ -1,27 +1,23 @@
1
1
  module ActiveMocker
2
- module Mock
2
+ class SingleRelation
3
3
 
4
- class SingleRelation
4
+ attr_reader :item
5
5
 
6
- attr_reader :item
7
-
8
- def initialize(item, child_self:, foreign_key:)
9
- @item = item
10
- assign_associations(child_self, item) if item.class <= Base
11
- end
6
+ def initialize(item, child_self:, foreign_key:)
7
+ @item = item
8
+ assign_associations(child_self, item) if item.class <= Base
9
+ end
12
10
 
13
- def assign_associations(child_self, item)
14
- [*item.class._find_associations_by_class(child_self.class.send('mocked_class'))].each do |type, relations|
15
- relations.each do |relation|
16
- if item.send(relation).class <= Collection
17
- item.send(relation) << child_self
18
- else
19
- item.send(:write_association, relation, child_self)
20
- end
11
+ def assign_associations(child_self, item)
12
+ [*item.class._find_associations_by_class(child_self.class.send('mocked_class'))].each do |type, relations|
13
+ relations.each do |relation|
14
+ if item.send(relation).class <= Collection
15
+ item.send(relation) << child_self
16
+ else
17
+ item.send(:write_association, relation, child_self)
21
18
  end
22
19
  end
23
20
  end
24
-
25
21
  end
26
22
 
27
23
  end
@@ -1,5 +1,4 @@
1
1
  module ActiveMocker
2
- module Mock
3
2
  module TemplateMethods
4
3
 
5
4
  def self.included(base)
@@ -13,7 +12,7 @@ module Mock
13
12
  end
14
13
 
15
14
  def types
16
- ActiveMocker::Mock::HashProcess.new({}, method(:build_type))
15
+ HashProcess.new({}, method(:build_type))
17
16
  end
18
17
 
19
18
  def associations
@@ -37,7 +36,5 @@ module Mock
37
36
  end
38
37
 
39
38
  end
40
-
41
39
  end
42
40
  end
43
- end
@@ -7,6 +7,7 @@ module ActiveMocker
7
7
  class_introspector: nil,
8
8
  enabled_partials: nil,
9
9
  klasses_to_be_mocked:,
10
+ mock_append_name:,
10
11
  active_record_base_klass: ActiveRecord::Base)
11
12
  @file = file
12
13
  @file_out = file_out
@@ -16,6 +17,7 @@ module ActiveMocker
16
17
  @enabled_partials = enabled_partials || self.class.enabled_partials_default
17
18
  @klasses_to_be_mocked = klasses_to_be_mocked
18
19
  @active_record_base_klass = active_record_base_klass
20
+ @mock_append_name = mock_append_name
19
21
  @errors = []
20
22
  @completed = false
21
23
  end
@@ -45,7 +47,8 @@ module ActiveMocker
45
47
  :class_introspector,
46
48
  :enabled_partials,
47
49
  :klasses_to_be_mocked,
48
- :active_record_base_klass
50
+ :active_record_base_klass,
51
+ :mock_append_name
49
52
 
50
53
  # -- Defaults -- #
51
54
  private
@@ -96,10 +99,6 @@ module ActiveMocker
96
99
  class_introspector.parsed_source.class_name
97
100
  end
98
101
 
99
- def mock_append_name
100
- 'Mock'
101
- end
102
-
103
102
  def parent_class
104
103
  @parent_class
105
104
  end
@@ -9,7 +9,7 @@ read_association(:<%= meth.name %>) || write_association(:<%= meth.name %>, clas
9
9
  end
10
10
  def <%= meth.name %>=(val)
11
11
  write_association(:<%= meth.name %>, val)
12
- ActiveMocker::Mock::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>).item
12
+ ActiveMocker::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>).item
13
13
  end
14
14
 
15
15
  def build_<%= meth.name %>(attributes={}, &block)
@@ -39,7 +39,7 @@ end
39
39
 
40
40
  def <%= meth.name %>=(val)
41
41
  write_association(:<%= meth.name %>, val)
42
- ActiveMocker::Mock::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>').item
42
+ ActiveMocker::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>').item
43
43
  end
44
44
 
45
45
  def build_<%= meth.name %>(attributes={}, &block)
@@ -62,21 +62,21 @@ end
62
62
 
63
63
  <% has_many.each do |meth| -%>
64
64
  def <%= meth.name %>
65
- read_association(:<%= meth.name %>, -> { ActiveMocker::Mock::HasMany.new([],foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>') })
65
+ read_association(:<%= meth.name %>, -> { ActiveMocker::HasMany.new([],foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>') })
66
66
  end
67
67
 
68
68
  def <%= meth.name %>=(val)
69
- write_association(:<%= meth.name %>, ActiveMocker::Mock::HasMany.new(val, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>'))
69
+ write_association(:<%= meth.name %>, ActiveMocker::HasMany.new(val, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>'))
70
70
  end
71
71
  <% end -%>
72
72
  <%= '# has_and_belongs_to_many' unless has_and_belongs_to_many.empty? -%>
73
73
 
74
74
  <% has_and_belongs_to_many.each do |meth| -%>
75
75
  def <%= meth.name %>
76
- read_association(:<%= meth.name %>, ->{ ActiveMocker::Mock::HasAndBelongsToMany.new([]) })
76
+ read_association(:<%= meth.name %>, ->{ ActiveMocker::HasAndBelongsToMany.new([]) })
77
77
  end
78
78
 
79
79
  def <%= meth.name %>=(val)
80
- write_association(:<%= meth.name %>, ActiveMocker::Mock::HasAndBelongsToMany.new(val))
80
+ write_association(:<%= meth.name %>, ActiveMocker::HasAndBelongsToMany.new(val))
81
81
  end
82
82
  <% end -%>
@@ -5,7 +5,7 @@
5
5
  end
6
6
 
7
7
  def types
8
- @types ||= ActiveMocker::Mock::HashProcess.new(<%= types_hash %>, method(:build_type)).merge(super)
8
+ @types ||= ActiveMocker::HashProcess.new(<%= types_hash %>, method(:build_type)).merge(super)
9
9
  end
10
10
 
11
11
  def associations
@@ -12,12 +12,12 @@ end
12
12
 
13
13
  extend Scopes
14
14
 
15
- class ScopeRelation < ActiveMocker::Mock::Association
15
+ class ScopeRelation < ActiveMocker::Association
16
16
  include <%= class_name + mock_append_name %>::Scopes
17
17
  end
18
18
 
19
- def self.new_relation(collection)
19
+ def self.__new_relation__(collection)
20
20
  <%= class_name + mock_append_name %>::ScopeRelation.new(collection)
21
21
  end
22
22
 
23
- private_class_method :new_relation
23
+ private_class_method :__new_relation__
@@ -22,7 +22,7 @@ module ActiveMocker
22
22
  if @parent_mock_name
23
23
  "#{@parent_mock_name}#{mock_append_name}"
24
24
  else
25
- 'ActiveMocker::Mock::Base'
25
+ 'ActiveMocker::Base'
26
26
  end
27
27
  end
28
28
 
@@ -8,13 +8,16 @@ module ActiveMocker
8
8
  # c.single_model_path= # Path to generate a single mock
9
9
  # c.progress_bar= # False disables progress bar from sending to STDOUT
10
10
  # or pass a class that takes a count in the initializer and responds to #increment.
11
- # c.error_verbosity= # 0 = none, 1 = One line per error, 2 = More details
11
+ # c.error_verbosity= # 0 = none
12
+ # # 1 = Summary of failures
13
+ # # 2 = One line per error
14
+ # # 3 = Errors with exception backtrace
12
15
  # c.disable_modules_and_constants= # Modules are include/extend along with constant declarations.
13
16
  # # Default is false, to disable set to true.
14
17
  # end
15
18
  #
16
- # @param [block]
17
19
  # @returns self
20
+ # @yield [Config]
18
21
  def configure(&block)
19
22
  Config.set(&block)
20
23
  self
@@ -1,16 +1,8 @@
1
1
  module ActiveMocker
2
2
  module Rspec
3
-
4
3
  # @return ActiveMocker::LoadedMocks
5
4
  def active_mocker
6
5
  ActiveMocker::LoadedMocks
7
6
  end
8
-
9
- # @deprecated method, will be removed in version 2.1
10
- # Use +active_mocker.mocks.find('ClassName')+ instead
11
- def mock_class(*args)
12
- active_mocker.mocks.find(*args)
13
- end
14
-
15
7
  end
16
8
  end
@@ -2,7 +2,6 @@ require 'active_mocker/loaded_mocks'
2
2
  require 'active_mocker/rspec'
3
3
 
4
4
  RSpec.configure do |config|
5
-
6
5
  config.include ActiveMocker::Rspec
7
6
 
8
7
  config.before(:each, active_mocker: true) do
@@ -18,5 +17,4 @@ RSpec.configure do |config|
18
17
  config.before(:all, active_mocker: true) do
19
18
  ActiveMocker::LoadedMocks.delete_all
20
19
  end
21
-
22
20
  end
@@ -27,5 +27,3 @@ end
27
27
  Rake::Task['active_mocker:build'].invoke
28
28
  end
29
29
  end
30
-
31
-
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "2.0.0.beta1"
2
+ VERSION = "2.0.0.pre1"
3
3
  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: 2.0.0.beta1
4
+ version: 2.0.0.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.5
33
+ version: '1.0'
34
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: 1.0.5
40
+ version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: ruby-progressbar
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +114,14 @@ dependencies:
100
114
  requirements:
101
115
  - - '='
102
116
  - !ruby/object:Gem::Version
103
- version: 0.1.5
117
+ version: 0.1.6
104
118
  type: :runtime
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - '='
109
123
  - !ruby/object:Gem::Version
110
- version: 0.1.5
124
+ version: 0.1.6
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: bundler
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -136,17 +150,18 @@ dependencies:
136
150
  - - "~>"
137
151
  - !ruby/object:Gem::Version
138
152
  version: '3.4'
139
- description: ActiveMocker creates mocks classes from ActiveRecord models. Allowing
140
- your test suite to run very fast by not loading Rails or a database. It parses the
141
- schema.rb and the defined methods on a model then generates a ruby file that can
142
- be included within a test. The mock file can be run by themselves and come with
143
- a partial implementation of ActiveRecord. Attributes and associations can be used
144
- just the same as in ActiveRecord. Methods will have the correct arguments but raise
145
- an NotImplementedError when called. Mocks are regenerated when the schema is modified
146
- so your mocks will not go stale; preventing the case where your units tests pass
147
- but production code fails.
153
+ description: ActiveMocker creates mock classes from ActiveRecord models, allowing
154
+ your test suite to run at breakneck speed. This can be done by not loading Rails
155
+ or hitting a database. The models are read dynamically and statically so that ActiveMocker
156
+ can generate a Ruby file to require within a test. The mock file can be run by itself
157
+ and comes with a partial implementation of ActiveRecord. Attributes and associations
158
+ can be used the same as in ActiveRecord. Methods have the same argument signature
159
+ but raise a NotImplementedError when called, allowing you to stub it with a mocking
160
+ framework, like RSpec. Mocks are regenerated when the schema is modified so your
161
+ mocks won't go stale, preventing the case where your units tests pass but production
162
+ code fails.
148
163
  email:
149
- - dustin@zive.me
164
+ - dustin@zeisler.net
150
165
  executables: []
151
166
  extensions: []
152
167
  extra_rdoc_files: []
@@ -156,9 +171,12 @@ files:
156
171
  - README.md
157
172
  - lib/active_mocker.rb
158
173
  - lib/active_mocker/config.rb
174
+ - lib/active_mocker/deprecated_components/mock_abilities.rb
175
+ - lib/active_mocker/deprecated_components/rspec.rb
176
+ - lib/active_mocker/display_errors.rb
177
+ - lib/active_mocker/error_object.rb
159
178
  - lib/active_mocker/generate.rb
160
179
  - lib/active_mocker/loaded_mocks.rb
161
- - lib/active_mocker/logger.rb
162
180
  - lib/active_mocker/mock.rb
163
181
  - lib/active_mocker/mock/association.rb
164
182
  - lib/active_mocker/mock/base.rb
@@ -170,7 +188,7 @@ files:
170
188
  - lib/active_mocker/mock/has_many.rb
171
189
  - lib/active_mocker/mock/has_one.rb
172
190
  - lib/active_mocker/mock/hash_process.rb
173
- - lib/active_mocker/mock/mock_abilities.rb
191
+ - lib/active_mocker/mock/mock_relation.rb
174
192
  - lib/active_mocker/mock/object_inspect.rb
175
193
  - lib/active_mocker/mock/queries.rb
176
194
  - lib/active_mocker/mock/records.rb
@@ -186,7 +204,6 @@ files:
186
204
  - lib/active_mocker/mock_template/_modules_constants.erb
187
205
  - lib/active_mocker/mock_template/_scopes.erb
188
206
  - lib/active_mocker/null_progress.rb
189
- - lib/active_mocker/output_capture.rb
190
207
  - lib/active_mocker/parent_class.rb
191
208
  - lib/active_mocker/progress.rb
192
209
  - lib/active_mocker/public_methods.rb