active_mocker 1.4.2 → 1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +35 -29
- data/lib/active_mocker.rb +3 -1
- data/lib/active_mocker/active_record.rb +27 -5
- data/lib/active_mocker/active_record/scope.rb +5 -5
- data/lib/active_mocker/active_record/unknown_module.rb +4 -4
- data/lib/active_mocker/generate.rb +36 -15
- data/lib/active_mocker/loaded_mocks.rb +60 -54
- data/lib/active_mocker/mock.rb +24 -0
- data/lib/active_mocker/mock/association.rb +6 -0
- data/lib/{active_mock → active_mocker/mock}/base.rb +43 -89
- data/lib/{active_mock → active_mocker/mock}/collection.rb +14 -7
- data/lib/{active_mock → active_mocker/mock}/do_nothing_active_record_methods.rb +7 -1
- data/lib/active_mocker/mock/exceptions.rb +25 -0
- data/lib/active_mocker/mock/has_and_belongs_to_many.rb +6 -0
- data/lib/active_mocker/mock/has_many.rb +60 -0
- data/lib/active_mocker/mock/hash_process.rb +19 -0
- data/lib/active_mocker/mock/mock_abilities.rb +98 -0
- data/lib/{active_mock → active_mocker/mock}/next_id.rb +7 -2
- data/lib/{active_mock → active_mocker/mock}/object_inspect.rb +3 -1
- data/lib/{active_mock → active_mocker/mock}/queries.rb +32 -29
- data/lib/active_mocker/mock/records.rb +62 -0
- data/lib/{active_mock → active_mocker/mock}/relation.rb +4 -3
- data/lib/active_mocker/mock/template_methods.rb +39 -0
- data/lib/active_mocker/mock_template.erb +45 -31
- data/lib/active_mocker/model_reader.rb +29 -0
- data/lib/active_mocker/model_schema.rb +23 -16
- data/lib/active_mocker/model_schema/generate.rb +19 -8
- data/lib/active_mocker/railtie.rb +17 -0
- data/lib/active_mocker/reparameterize.rb +2 -4
- data/lib/active_mocker/rspec_helper.rb +18 -0
- data/lib/active_mocker/task.rake +15 -0
- data/lib/active_mocker/version.rb +1 -1
- metadata +80 -206
- data/.gitignore +0 -19
- data/.hound.yml +0 -0
- data/.travis.yml +0 -5
- data/Gemfile +0 -4
- data/Rakefile +0 -26
- data/active_mocker.gemspec +0 -40
- data/lib/active_mock/association.rb +0 -7
- data/lib/active_mock/creators.rb +0 -25
- data/lib/active_mock/has_and_belongs_to_many.rb +0 -7
- data/lib/active_mock/has_many.rb +0 -54
- data/lib/active_mock/records.rb +0 -83
- data/lib/active_mocker/active_mock.rb +0 -26
- data/lib/active_mocker/active_record/const_missing.rb +0 -5
- data/lib/active_mocker/class_exists.rb +0 -12
- data/lib/active_mocker/const_sets.rb +0 -24
- data/lib/active_mocker/mock_task.rb +0 -12
- data/sample_app_rails_4/.idea/.generators +0 -8
- data/sample_app_rails_4/.idea/.name +0 -1
- data/sample_app_rails_4/.idea/.rakeTasks +0 -7
- data/sample_app_rails_4/.idea/dataSources.ids +0 -141
- data/sample_app_rails_4/.idea/dataSources.xml +0 -36
- data/sample_app_rails_4/.idea/dictionaries/zeisler.xml +0 -3
- data/sample_app_rails_4/.idea/encodings.xml +0 -5
- data/sample_app_rails_4/.idea/inspectionProfiles/Project_Default.xml +0 -20
- data/sample_app_rails_4/.idea/inspectionProfiles/profiles_settings.xml +0 -7
- data/sample_app_rails_4/.idea/misc.xml +0 -5
- data/sample_app_rails_4/.idea/modules.xml +0 -10
- data/sample_app_rails_4/.idea/runConfigurations/Development__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/runConfigurations/Production__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/runConfigurations/spec__sample_app_rails_4.xml +0 -26
- data/sample_app_rails_4/.idea/runConfigurations/test__sample_app_rails_4.xml +0 -28
- data/sample_app_rails_4/.idea/sample_app_rails_4.iml +0 -262
- data/sample_app_rails_4/.idea/scopes/scope_settings.xml +0 -5
- data/sample_app_rails_4/.idea/vcs.xml +0 -7
- data/sample_app_rails_4/.idea/workspace.xml +0 -619
- data/sample_app_rails_4/.rspec +0 -1
- data/sample_app_rails_4/.secret +0 -1
- data/sample_app_rails_4/Gemfile +0 -13
- data/sample_app_rails_4/Guardfile +0 -53
- data/sample_app_rails_4/LICENSE +0 -21
- data/sample_app_rails_4/README.md +0 -25
- data/sample_app_rails_4/README.nitrous.md +0 -20
- data/sample_app_rails_4/Rakefile +0 -6
- data/sample_app_rails_4/app/assets/images/rails.png +0 -0
- data/sample_app_rails_4/app/assets/javascripts/application.js +0 -17
- data/sample_app_rails_4/app/assets/javascripts/sessions.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/javascripts/static_pages.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/javascripts/users.js.coffee +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/application.css +0 -13
- data/sample_app_rails_4/app/assets/stylesheets/custom.css.scss +0 -246
- data/sample_app_rails_4/app/assets/stylesheets/sessions.css.scss +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/static_pages.css.scss +0 -3
- data/sample_app_rails_4/app/assets/stylesheets/users.css.scss +0 -3
- data/sample_app_rails_4/app/controllers/application_controller.rb +0 -6
- data/sample_app_rails_4/app/controllers/concerns/.keep +0 -0
- data/sample_app_rails_4/app/controllers/microposts_controller.rb +0 -31
- data/sample_app_rails_4/app/controllers/relationships_controller.rb +0 -21
- data/sample_app_rails_4/app/controllers/sessions_controller.rb +0 -21
- data/sample_app_rails_4/app/controllers/static_pages_controller.rb +0 -18
- data/sample_app_rails_4/app/controllers/users_controller.rb +0 -80
- data/sample_app_rails_4/app/helpers/application_helper.rb +0 -12
- data/sample_app_rails_4/app/helpers/sessions_helper.rb +0 -49
- data/sample_app_rails_4/app/helpers/static_pages_helper.rb +0 -2
- data/sample_app_rails_4/app/helpers/users_helper.rb +0 -10
- data/sample_app_rails_4/app/mailers/.keep +0 -0
- data/sample_app_rails_4/app/models/.keep +0 -0
- data/sample_app_rails_4/app/models/concerns/.keep +0 -0
- data/sample_app_rails_4/app/models/micropost.rb +0 -24
- data/sample_app_rails_4/app/models/relationship.rb +0 -6
- data/sample_app_rails_4/app/models/user.rb +0 -39
- data/sample_app_rails_4/app/views/layouts/_footer.html.erb +0 -13
- data/sample_app_rails_4/app/views/layouts/_header.html.erb +0 -31
- data/sample_app_rails_4/app/views/layouts/_shim.html.erb +0 -3
- data/sample_app_rails_4/app/views/layouts/application.html.erb +0 -22
- data/sample_app_rails_4/app/views/microposts/_micropost.html.erb +0 -11
- data/sample_app_rails_4/app/views/relationships/create.js.erb +0 -2
- data/sample_app_rails_4/app/views/relationships/destroy.js.erb +0 -2
- data/sample_app_rails_4/app/views/sessions/new.html.erb +0 -19
- data/sample_app_rails_4/app/views/shared/_error_messages.html.erb +0 -12
- data/sample_app_rails_4/app/views/shared/_feed.html.erb +0 -6
- data/sample_app_rails_4/app/views/shared/_feed_item.html.erb +0 -15
- data/sample_app_rails_4/app/views/shared/_micropost_form.html.erb +0 -7
- data/sample_app_rails_4/app/views/shared/_stats.html.erb +0 -15
- data/sample_app_rails_4/app/views/shared/_user_info.html.erb +0 -12
- data/sample_app_rails_4/app/views/static_pages/about.html.erb +0 -8
- data/sample_app_rails_4/app/views/static_pages/contact.html.erb +0 -6
- data/sample_app_rails_4/app/views/static_pages/help.html.erb +0 -8
- data/sample_app_rails_4/app/views/static_pages/home.html.erb +0 -34
- data/sample_app_rails_4/app/views/static_pages/show.html.erb +0 -0
- data/sample_app_rails_4/app/views/users/_follow.html.erb +0 -5
- data/sample_app_rails_4/app/views/users/_follow_form.html.erb +0 -9
- data/sample_app_rails_4/app/views/users/_unfollow.html.erb +0 -5
- data/sample_app_rails_4/app/views/users/_user.html.erb +0 -8
- data/sample_app_rails_4/app/views/users/edit.html.erb +0 -27
- data/sample_app_rails_4/app/views/users/index.html.erb +0 -10
- data/sample_app_rails_4/app/views/users/new.html.erb +0 -24
- data/sample_app_rails_4/app/views/users/show.html.erb +0 -24
- data/sample_app_rails_4/app/views/users/show_follow.html.erb +0 -30
- data/sample_app_rails_4/bin/bundle +0 -3
- data/sample_app_rails_4/bin/rails +0 -4
- data/sample_app_rails_4/bin/rake +0 -4
- data/sample_app_rails_4/bin/rspec +0 -16
- data/sample_app_rails_4/config.ru +0 -4
- data/sample_app_rails_4/config/application.rb +0 -28
- data/sample_app_rails_4/config/boot.rb +0 -4
- data/sample_app_rails_4/config/database.yml +0 -27
- data/sample_app_rails_4/config/environment.rb +0 -5
- data/sample_app_rails_4/config/environments/development.rb +0 -25
- data/sample_app_rails_4/config/environments/production.rb +0 -79
- data/sample_app_rails_4/config/environments/test.rb +0 -38
- data/sample_app_rails_4/config/initializers/active_mocker.rb +0 -12
- data/sample_app_rails_4/config/initializers/backtrace_silencers.rb +0 -7
- data/sample_app_rails_4/config/initializers/filter_parameter_logging.rb +0 -4
- data/sample_app_rails_4/config/initializers/inflections.rb +0 -16
- data/sample_app_rails_4/config/initializers/mime_types.rb +0 -5
- data/sample_app_rails_4/config/initializers/secret_token.rb +0 -22
- data/sample_app_rails_4/config/initializers/session_store.rb +0 -3
- data/sample_app_rails_4/config/initializers/wrap_parameters.rb +0 -14
- data/sample_app_rails_4/config/locales/en.yml +0 -23
- data/sample_app_rails_4/config/routes.rb +0 -17
- data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +0 -10
- data/sample_app_rails_4/db/migrate/20130311194153_add_index_to_users_email.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130311201841_add_password_digest_to_users.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130314184954_add_remember_token_to_users.rb +0 -6
- data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +0 -5
- data/sample_app_rails_4/db/migrate/20130315175534_create_microposts.rb +0 -11
- data/sample_app_rails_4/db/migrate/20130315230445_create_relationships.rb +0 -13
- data/sample_app_rails_4/db/schema.rb +0 -51
- data/sample_app_rails_4/db/seeds.rb +0 -7
- data/sample_app_rails_4/features/signing_in.feature +0 -13
- data/sample_app_rails_4/features/step_definitions/authentication_steps.rb +0 -30
- data/sample_app_rails_4/features/support/env.rb +0 -59
- data/sample_app_rails_4/lib/assets/.keep +0 -0
- data/sample_app_rails_4/lib/tasks/.keep +0 -0
- data/sample_app_rails_4/lib/tasks/active_mocker.rake +0 -10
- data/sample_app_rails_4/lib/tasks/cucumber.rake +0 -65
- data/sample_app_rails_4/lib/tasks/sample_data.rake +0 -42
- data/sample_app_rails_4/lib/unit_logger.rb +0 -22
- data/sample_app_rails_4/public/404.html +0 -27
- data/sample_app_rails_4/public/422.html +0 -26
- data/sample_app_rails_4/public/500.html +0 -26
- data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css +0 -5091
- data/sample_app_rails_4/public/assets/application-4962059d8f80f9bb096692bacc29c4e8.css.gz +0 -0
- data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js +0 -12952
- data/sample_app_rails_4/public/assets/application-eeb856e3fe2c8f879c91d0e81d59cb40.js.gz +0 -0
- data/sample_app_rails_4/public/assets/glyphicons-halflings-c806376f05e4ccabe2c5315a8e95667c.png +0 -0
- data/sample_app_rails_4/public/assets/glyphicons-halflings-white-62b67d9edee3db90d18833087f848d6e.png +0 -0
- data/sample_app_rails_4/public/assets/manifest-802de9eb1c853769101852422b620883.json +0 -1
- data/sample_app_rails_4/public/assets/rails-231a680f23887d9dd70710ea5efd3c62.png +0 -0
- data/sample_app_rails_4/public/favicon.ico +0 -0
- data/sample_app_rails_4/public/robots.txt +0 -5
- data/sample_app_rails_4/script/cucumber +0 -10
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +0 -1038
- data/sample_app_rails_4/spec/factories.rb +0 -17
- data/sample_app_rails_4/spec/micropost_mock_spec.rb +0 -145
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +0 -153
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +0 -151
- data/sample_app_rails_4/spec/mocks/user_mock.rb +0 -215
- data/sample_app_rails_4/spec/reload_spec.rb +0 -45
- data/sample_app_rails_4/spec/spec_helper.rb +0 -46
- data/sample_app_rails_4/spec/user_mock_spec.rb +0 -197
- data/sample_app_rails_4/vendor/assets/javascripts/.keep +0 -0
- data/sample_app_rails_4/vendor/assets/stylesheets/.keep +0 -0
- data/spec/lib/acitve_mock/queriable_spec.rb +0 -207
- data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +0 -124
- data/spec/lib/active_mocker/generate_spec.rb +0 -40
- data/spec/lib/active_mocker/loaded_mocks_spec.rb +0 -167
- data/spec/lib/active_mocker/logger_spec.rb +0 -32
- data/spec/lib/active_mocker/model_reader_spec.rb +0 -206
- data/spec/lib/active_mocker/model_schema/generate_spec.rb +0 -111
- data/spec/lib/active_mocker/model_schema_spec.rb +0 -145
- data/spec/lib/active_mocker/schema_reader_spec.rb +0 -109
- data/spec/lib/model.rb +0 -43
- data/spec/lib/person.rb +0 -9
- data/spec/lib/readme_spec.rb +0 -199
- data/spec/lib/reparameterize_spec.rb +0 -202
- data/spec/lib/schema.rb +0 -40
- data/spec/unit_logger.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4906c7473f9d0958f72a17b3ffea9fedd8675e40
|
4
|
+
data.tar.gz: 37a17866216a3179a00c00698dd40a439162ce03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b16c13ff134b3bd3c4c78629a69ef70ffdf58fcfd253a16207ef742ff22fc344361e4189a6dfc74ec6dca7dbf9136f38c22bd394d0dddbb3da121ab05328fb6
|
7
|
+
data.tar.gz: 8fe0cbcfc25c4540fd1476ab4644d0caeead8222ba6f4bdb86787161935a0c0b8a0e7d3c7b7730cf6fdfd4fe472ec24dde8f605d2c476008f9155e8369065742
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
## 1.5 - 2014-07-07
|
5
|
+
|
6
|
+
### Added
|
7
|
+
- Modules that are included/extended in ActiveRecord Models are now available. This may cause failing tests, requiring the modules will fix the issues.
|
8
|
+
- Scoped methods from association will be available if the associated mock is loaded.
|
9
|
+
- rspec_helper.rb
|
10
|
+
- Using `tag active_mocker:true` will stub ActiveRecord Model constants for Mock constants. To use with `before(:all)` wrap Constant in `#mock_class('ClassName')`. Using all AR Model names will allow a spec file to run as a full Rails test and as a unit test.
|
11
|
+
- Tag will also clear all mocks after(:all)
|
12
|
+
|
13
|
+
### Deprecated
|
14
|
+
- mock_class_method and mock_instance_method are deprecated and will be removed in 2.0. Rspec 3 mocks has a better implantation of this feature.
|
15
|
+
|
16
|
+
### Removed
|
17
|
+
- Nothing.
|
18
|
+
|
19
|
+
### Fixed
|
20
|
+
- `has_many#create_assocation` will now correctly add it self to the parent record.
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# ActiveMocker
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/active_mocker.svg)](http://badge.fury.io/rb/active_mocker)
|
2
3
|
[![Build Status](https://travis-ci.org/zeisler/active_mocker.png?branch=master)](https://travis-ci.org/zeisler/active_mocker)
|
3
4
|
[![Code Climate](https://codeclimate.com/github/zeisler/active_mocker.png)](https://codeclimate.com/github/zeisler/active_mocker)
|
5
|
+
[![Dependency Status](https://gemnasium.com/zeisler/active_mocker.svg)](https://gemnasium.com/zeisler/active_mocker)
|
6
|
+
[![Gitter chat](https://badges.gitter.im/zeisler/active_mocker.png)](https://gitter.im/zeisler/active_mocker)
|
4
7
|
|
5
8
|
ActiveMocker creates mocks classes from ActiveRecord models. Allowing your test suite to run very fast by not loading Rails or hooking to a database. It parses the schema definition and the defined methods on a model then saves a ruby file that can be included within a test. Mocks are regenerated when the schema is modified so your mocks will not go stale. This prevents the case where your units tests pass but production code is failing.
|
6
9
|
|
@@ -11,9 +14,11 @@ Example from a real app
|
|
11
14
|
|
12
15
|
|
13
16
|
------------------------------------------
|
14
|
-
|
17
|
+
* [Contact](#contact)
|
15
18
|
* [Installation](#installation)
|
16
19
|
* [Setup](#setup)
|
20
|
+
* [Configuration](#overwrite_defaults_configuration)
|
21
|
+
* [Generate](#generate_mocks)
|
17
22
|
* [Dependencies](#dependencies)
|
18
23
|
* [Usage](#usage)
|
19
24
|
* [Mocking Methods](#mocking-methods)
|
@@ -26,6 +31,12 @@ Example from a real app
|
|
26
31
|
|
27
32
|
------------------------------------------
|
28
33
|
|
34
|
+
## Contact
|
35
|
+
|
36
|
+
Ask a question in the [chat room](https://gitter.im/zeisler/active_mocker).
|
37
|
+
|
38
|
+
------------------------
|
39
|
+
|
29
40
|
|
30
41
|
## Installation
|
31
42
|
|
@@ -48,34 +59,20 @@ Or install it yourself as:
|
|
48
59
|
|
49
60
|
## Setup
|
50
61
|
|
51
|
-
###
|
52
|
-
config/initializers/active_mocker.rb
|
62
|
+
### Overwrite defaults configuration
|
53
63
|
|
54
64
|
ActiveMocker::Generate.configure do |config|
|
55
|
-
# Required Options
|
56
65
|
config.schema_file = File.join(Rails.root, 'db/schema.rb')
|
57
66
|
config.model_dir = File.join(Rails.root, 'app/models')
|
58
67
|
config.mock_dir = File.join(Rails.root, 'spec/mocks')
|
59
|
-
# Logging
|
60
68
|
config.logger = Rails.logger
|
61
69
|
end
|
62
70
|
|
63
|
-
###
|
71
|
+
### Generate Mocks
|
64
72
|
|
65
|
-
|
73
|
+
Running this rake task builds/rebuilds the mocks. It will be ran automatically after every schema modification. If the model changes this rake task needs to be called manually. You could add a file watcher for when your models change and have it run the rake task.
|
66
74
|
|
67
|
-
|
68
|
-
|
69
|
-
task rebuild_mocks: :environment do
|
70
|
-
puts 'rebuilding mocks'
|
71
|
-
ActiveMocker.create_mocks
|
72
|
-
end
|
73
|
-
|
74
|
-
['db:schema:load', 'db:migrate', 'db:reset'].each do |task|
|
75
|
-
Rake::Task[task].enhance do
|
76
|
-
Rake::Task['rebuild_mocks'].invoke
|
77
|
-
end
|
78
|
-
end
|
75
|
+
rake active_mocker::build
|
79
76
|
|
80
77
|
## Usage
|
81
78
|
|
@@ -158,7 +155,8 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
158
155
|
PersonMock.bar('baz')
|
159
156
|
=> RuntimeError: ::bar is not Implemented for Class: PersonMock
|
160
157
|
|
161
|
-
|
158
|
+
# Rspec 3 Mocks
|
159
|
+
allow(PersonMock).to receive(:bar) do |name, type=nil|
|
162
160
|
"Now implemented with #{name} and #{type}"
|
163
161
|
end
|
164
162
|
|
@@ -168,14 +166,11 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
168
166
|
PersonMock.new.bar('foo', 'type')
|
169
167
|
=> "Now implemented with foo and type"
|
170
168
|
|
171
|
-
|
169
|
+
# Rspec 3 Mocks
|
170
|
+
allow_any_instance(PersonMock).to receive(:bar) do
|
172
171
|
"Now implemented"
|
173
172
|
end
|
174
173
|
|
175
|
-
# override mock on an individual instance
|
176
|
-
PersonMock.new.mock_instance_method(:bar) do
|
177
|
-
"Now implemented!!!!"
|
178
|
-
end
|
179
174
|
|
180
175
|
#### When the model changes, the mock fails
|
181
176
|
(Requires a regeneration of the mocks files.)
|
@@ -215,13 +210,22 @@ Here is an example of a rake task to regenerate mocks after every schema modifia
|
|
215
210
|
|
216
211
|
#person_spec.rb
|
217
212
|
|
218
|
-
|
213
|
+
# Rspec 3 Mocks
|
214
|
+
allow(person_mock).to receive(:bar) do |name, type=nil|
|
219
215
|
"Now implemented with #{name} and #{type}"
|
220
216
|
end
|
221
217
|
=> NoMethodError: undefined method `bar' for class `PersonMock'
|
222
218
|
|
223
219
|
### Managing Mocks
|
224
220
|
|
221
|
+
Rspec Tag - active_mocker:true
|
222
|
+
|
223
|
+
describe 'Example', active_mocker:true do
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
Assigning this tag will stub any ActiveRecord model Constants for Mock classes in any `it's` or `before(:each)`. To stub any Constants in `before(:all)`, `after(:all)` use `mock_class('ClassName')`.
|
228
|
+
|
225
229
|
Deletes All Records and Clears Mocked Methods
|
226
230
|
|
227
231
|
PersonMock.clear_mock
|
@@ -243,8 +247,10 @@ Map The Mock Class to it's Model
|
|
243
247
|
|
244
248
|
ActiveMocker::LoadedMocks.class_name_to_mock
|
245
249
|
=> { 'Person' => PersonMock }
|
250
|
+
|
251
|
+
|
246
252
|
|
247
|
-
### Constants
|
253
|
+
### Constants and included and extended Modules are Available.
|
248
254
|
|
249
255
|
#app/models/person.rb
|
250
256
|
|
@@ -338,9 +344,9 @@ Map The Mock Class to it's Model
|
|
338
344
|
|
339
345
|
### Known Limitations
|
340
346
|
* Model names and table names must follow the default ActiveRecord naming pattern.
|
341
|
-
*
|
342
|
-
* Whatever associations are setup in one mock object will not effect any other objects.
|
347
|
+
* Whatever associations are setup in one mock object will not reflected in any other objects.
|
343
348
|
* Validation are not present in mocks.
|
349
|
+
* Sql queries, joins, etc will never be supported.
|
344
350
|
|
345
351
|
## Inspiration
|
346
352
|
Thanks to Jeff Olfert for being my original inspiration for this project.
|
data/lib/active_mocker.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "active_mocker/version"
|
2
|
-
$:.unshift File.expand_path('../../', __FILE__)
|
3
2
|
|
3
|
+
require 'active_mocker/railtie' if defined?(Rails)
|
4
|
+
require 'ruby-progressbar'
|
5
|
+
require 'forwardable'
|
4
6
|
require 'active_mocker/public_methods'
|
5
7
|
require 'active_mocker/config'
|
6
8
|
require 'active_mocker/generate'
|
@@ -3,16 +3,14 @@ require 'active_mocker/active_record/scope'
|
|
3
3
|
require 'active_mocker/active_record/relationships'
|
4
4
|
require 'active_mocker/active_record/unknown_class_method'
|
5
5
|
require 'active_mocker/active_record/unknown_module'
|
6
|
-
require 'active_mocker/active_record/const_missing'
|
7
6
|
|
8
7
|
module ActiveMocker
|
9
8
|
module ActiveRecord
|
10
|
-
class Base
|
9
|
+
class Base #< ::ActiveRecord::Base
|
11
10
|
extend Scope
|
12
11
|
extend Relationships
|
13
12
|
extend UnknownClassMethod
|
14
|
-
extend UnknownModule
|
15
|
-
extend ConstMissing
|
13
|
+
# extend UnknownModule
|
16
14
|
|
17
15
|
def self.table_name=(table_name)
|
18
16
|
@table_name = table_name
|
@@ -32,8 +30,32 @@ module ActiveMocker
|
|
32
30
|
@primary_key
|
33
31
|
end
|
34
32
|
|
33
|
+
class ConstMissing
|
34
|
+
|
35
|
+
def self.const_missing(name)
|
36
|
+
self.const_set name, Class.new(ConstMissing)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
35
41
|
def self.const_missing(name)
|
36
|
-
|
42
|
+
Object.const_set name, Class.new(ConstMissing)
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.include(name)
|
46
|
+
_included << name
|
47
|
+
end
|
48
|
+
|
49
|
+
def self._included
|
50
|
+
@included ||= []
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.extend(name)
|
54
|
+
_extended << name
|
55
|
+
end
|
56
|
+
|
57
|
+
def self._extended
|
58
|
+
@extended ||= []
|
37
59
|
end
|
38
60
|
end
|
39
61
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Scope
|
2
2
|
|
3
3
|
def scope(method_name, proc)
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
get_named_scopes[method_name] = proc
|
5
|
+
end
|
6
|
+
|
7
|
+
def get_named_scopes
|
8
|
+
@scope_methods ||= {}
|
9
9
|
end
|
10
10
|
|
11
11
|
end
|
@@ -3,20 +3,20 @@ module ActiveMocker
|
|
3
3
|
module UnknownModule
|
4
4
|
|
5
5
|
def include(_module)
|
6
|
-
try_and_log('include', _module)
|
6
|
+
try_and_log('include', _module, caller)
|
7
7
|
end
|
8
8
|
|
9
9
|
def extend(_module)
|
10
|
-
try_and_log('extend', _module)
|
10
|
+
try_and_log('extend', _module, caller)
|
11
11
|
end
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
def try_and_log(type, name)
|
15
|
+
def try_and_log(type, name, _caller)
|
16
16
|
begin
|
17
17
|
super _module
|
18
18
|
rescue => e
|
19
|
-
Logger.debug "ActiveMocker :: Debug :: Can't #{type} module #{name} from class #{self.name}.\n\t\t\t\t\t\t\t\t#{
|
19
|
+
Logger.debug "ActiveMocker :: Debug :: Can't #{type} module #{name} from class #{self.name}.\n\t\t\t\t\t\t\t\t#{_caller}\n"
|
20
20
|
Logger.debug "\t\t\t\t\t\t\t\t#{e}"
|
21
21
|
end
|
22
22
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'ruby-progressbar'
|
2
|
+
require 'forwardable'
|
1
3
|
module ActiveMocker
|
2
4
|
class Generate
|
3
5
|
extend Config
|
@@ -14,7 +16,10 @@ class Generate
|
|
14
16
|
:mock_dir,
|
15
17
|
:logger
|
16
18
|
|
17
|
-
|
19
|
+
attr_reader :silence
|
20
|
+
|
21
|
+
def initialize(silence: false)
|
22
|
+
@silence = silence
|
18
23
|
create_template
|
19
24
|
end
|
20
25
|
|
@@ -32,30 +37,46 @@ class Generate
|
|
32
37
|
end
|
33
38
|
|
34
39
|
def generate_model_schema
|
35
|
-
ActiveMocker::ModelSchema::Generate.new(schema_file: schema_file, models_dir: model_dir, logger: logger).run
|
40
|
+
ActiveMocker::ModelSchema::Generate.new(schema_file: schema_file, models_dir: model_dir, logger: logger, progress: progress).run
|
41
|
+
end
|
42
|
+
|
43
|
+
def model_count
|
44
|
+
ActiveMocker::ModelSchema::Generate.new(schema_file: schema_file, models_dir: model_dir, logger: logger).models.count
|
45
|
+
end
|
46
|
+
|
47
|
+
def progress
|
48
|
+
return @progress if !@progress.nil? || silence == true
|
49
|
+
progress_options = {:title => "Generating Mocks",
|
50
|
+
:total => model_count * 2,
|
51
|
+
format: '%t |%b>>%i| %p%%'}
|
52
|
+
@progress = ProgressBar.create(progress_options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def increment_progress
|
56
|
+
progress.increment unless silence
|
36
57
|
end
|
37
58
|
|
38
59
|
def create_template
|
39
60
|
mocks_created = 0
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
klass_str = model.render(File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read, mock_append_name)
|
61
|
+
generate_model_schema.each do |model|
|
62
|
+
begin
|
63
|
+
|
64
|
+
klass_str = model.render(File.open(File.join(File.expand_path('../', __FILE__), 'mock_template.erb')).read, mock_append_name)
|
45
65
|
FileUtils::mkdir_p mock_dir unless File.directory? mock_dir
|
46
66
|
File.open(File.join(mock_dir,"#{model.table_name.singularize}_mock.rb"), 'w').write(klass_str)
|
47
67
|
logger.info "saving mock #{model.class_name} to #{mock_dir}"
|
48
68
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
69
|
+
rescue Exception => exception
|
70
|
+
logger.debug $!.backtrace
|
71
|
+
logger.debug exception
|
72
|
+
logger.info "failed to load #{model} model"
|
73
|
+
next
|
74
|
+
end
|
55
75
|
mocks_created += 1
|
56
|
-
|
76
|
+
increment_progress
|
57
77
|
end
|
58
|
-
|
78
|
+
progress.finish unless silence
|
79
|
+
logger.info "Generated #{mocks_created} of #{model_count} mocks"
|
59
80
|
end
|
60
81
|
|
61
82
|
def mock_append_name
|
@@ -1,75 +1,81 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
|
3
3
|
class LoadedMocks
|
4
|
-
|
5
|
-
|
6
|
-
end
|
4
|
+
|
5
|
+
class << self
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def add(mocks_to_add)
|
8
|
+
mocks.merge!({mocks_to_add.name => mocks_to_add})
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def add_subclass(subclass)
|
12
|
+
subclasses.merge!({subclass.mocked_class => subclass})
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def subclasses
|
16
|
+
@subclasses ||= {}
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def clear_subclasses
|
20
|
+
subclasses.clear
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def mocks
|
24
|
+
@mocks ||= {}
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
def find(klass)
|
28
|
+
class_name_to_mock[klass]
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
action.call(subclasses)
|
36
|
-
clear_subclasses
|
37
|
-
end
|
31
|
+
def all
|
32
|
+
mocks
|
33
|
+
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
35
|
+
def clear_all
|
36
|
+
all_mocks.each { |m| m.clear_mock }
|
37
|
+
clear_subclasses
|
38
|
+
end
|
44
39
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
action.call(subclasses)
|
49
|
-
end
|
40
|
+
def delete_all
|
41
|
+
all_mocks.each { |m| m.delete_all }
|
42
|
+
end
|
50
43
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end }
|
55
|
-
action.call(mocks)
|
56
|
-
action.call(subclasses)
|
57
|
-
end
|
44
|
+
def reload_all
|
45
|
+
all_mocks.each { |m| m.send(:reload) }
|
46
|
+
end
|
58
47
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
48
|
+
def undefine_all
|
49
|
+
all_mocks_as_str.each do |n|
|
50
|
+
Object.send(:remove_const, n) if Object.const_defined?(n)
|
51
|
+
end
|
63
52
|
end
|
64
|
-
hash.merge(subclasses)
|
65
|
-
end
|
66
53
|
|
67
|
-
|
54
|
+
def all_mocks_as_str
|
55
|
+
mocks.keys + subclasses.keys
|
56
|
+
end
|
68
57
|
|
69
|
-
|
70
|
-
|
71
|
-
|
58
|
+
def class_name_to_mock
|
59
|
+
hash = mocks.values.each_with_object({}) do |mock_constant, hash|
|
60
|
+
hash[mock_constant.mocked_class] = mock_constant
|
61
|
+
end
|
62
|
+
hash.merge(subclasses)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def internal_clear
|
68
|
+
clear_subclasses
|
69
|
+
mocks.clear
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def all_mocks
|
74
|
+
mocks.values + subclasses.values
|
75
|
+
end
|
76
|
+
|
72
77
|
end
|
78
|
+
|
73
79
|
end
|
74
80
|
|
75
81
|
end
|