active_mocker 1.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +61 -25
  4. data/Rakefile +1 -1
  5. data/active_mocker.gemspec +3 -3
  6. data/lib/active_hash/init.rb +13 -22
  7. data/lib/active_mocker.rb +1 -0
  8. data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
  9. data/lib/active_mocker/active_record/unknown_module.rb +3 -3
  10. data/lib/active_mocker/collection_association.rb +19 -20
  11. data/lib/active_mocker/config.rb +7 -4
  12. data/lib/active_mocker/field.rb +4 -4
  13. data/lib/active_mocker/generate.rb +75 -18
  14. data/lib/active_mocker/logger.rb +10 -1
  15. data/lib/active_mocker/mock_class_methods.rb +12 -2
  16. data/lib/active_mocker/mock_instance_methods.rb +3 -2
  17. data/lib/active_mocker/mock_requires.rb +2 -1
  18. data/lib/active_mocker/mock_template.erb +25 -22
  19. data/lib/active_mocker/public_methods.rb +6 -2
  20. data/lib/active_mocker/version.rb +1 -1
  21. data/sample_app_rails_4/app/models/micropost.rb +0 -2
  22. data/sample_app_rails_4/bin/rspec +16 -0
  23. data/sample_app_rails_4/config/application.rb +0 -3
  24. data/sample_app_rails_4/config/database.yml +5 -6
  25. data/sample_app_rails_4/config/environments/development.rb +0 -2
  26. data/sample_app_rails_4/config/environments/test.rb +0 -1
  27. data/sample_app_rails_4/config/initializers/active_mocker.rb +7 -5
  28. data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +1 -1
  29. data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +1 -1
  30. data/sample_app_rails_4/db/schema.rb +4 -3
  31. data/sample_app_rails_4/lib/tasks/{mocks.rake → active_mocker.rake} +5 -5
  32. data/sample_app_rails_4/lib/unit_logger.rb +22 -0
  33. data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +110 -4
  34. data/sample_app_rails_4/spec/mocks/micropost_mock.rb +31 -27
  35. data/sample_app_rails_4/spec/mocks/relationship_mock.rb +29 -24
  36. data/sample_app_rails_4/spec/mocks/user_mock.rb +69 -58
  37. data/sample_app_rails_4/spec/spec_helper.rb +6 -7
  38. data/sample_app_rails_4/spec/user_mock_spec.rb +14 -7
  39. data/spec/lib/active_mocker/collection_association_spec.rb +17 -3
  40. data/spec/lib/active_mocker/generate_spec.rb +8 -6
  41. data/spec/lib/active_mocker/model_reader_spec.rb +5 -0
  42. data/spec/lib/active_mocker/schema_reader_spec.rb +1 -1
  43. data/spec/lib/readme_spec.rb +199 -205
  44. data/spec/unit_logger.rb +24 -0
  45. metadata +22 -32
  46. data/mocks/micropost_mock.rb +0 -108
  47. data/mocks/relationship_mock.rb +0 -109
  48. data/mocks/user_mock.rb +0 -199
  49. data/plan_mock.rb +0 -2323
  50. data/sample_app_rails_4/config/cucumber.yml +0 -8
  51. data/sample_app_rails_4/db/development.sqlite3 +0 -0
  52. data/sample_app_rails_4/db/test.sqlite3 +0 -0
  53. data/spec/lib/active_mocker/performance/base_spec.rb +0 -454
  54. data/spec/lib/active_mocker/performance/large_schema.rb +0 -3576
  55. data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
  56. data/spec/lib/active_mocker/performance/schema_reader_spec.rb +0 -96
  57. data/spec/lib/compare_mocker_and_record_spec.rb +0 -133
@@ -1,9 +1,8 @@
1
- require 'rubygems'
1
+ ENV["RAILS_ENV"] ||= 'test'
2
2
 
3
- ENV["RAILS_ENV"] ||= 'test'
4
- require File.expand_path("../../config/environment", __FILE__)
5
- require 'rspec/rails'
6
- require 'rspec/autorun'
3
+ require File.expand_path("../../config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'rspec/autorun'
7
6
 
8
7
  # Requires supporting ruby files with custom matchers and macros, etc,
9
8
  # in spec/support/ and its subdirectories.
@@ -11,9 +10,9 @@ require 'rubygems'
11
10
 
12
11
  # Checks for pending migrations before tests are run.
13
12
  # If you are not using ActiveRecord, you can remove this line.
14
- ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
13
+ ActiveRecord::Migration.maintain_test_schema!
15
14
 
16
- RSpec.configure do |config|
15
+ RSpec.configure do |config|
17
16
  # ## Mock Framework
18
17
  #
19
18
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
@@ -1,15 +1,21 @@
1
1
  require 'rspec'
2
- $:.unshift File.expand_path('../', __FILE__)
3
- load 'mocks/user_mock.rb'
2
+ $:.unshift File.expand_path('../../', __FILE__)
3
+ APP_ROOT = File.expand_path('../../', __FILE__)
4
+
5
+ require 'config/initializers/active_mocker.rb'
6
+ load 'spec/mocks/user_mock.rb'
4
7
 
5
8
  describe UserMock do
6
9
 
7
- before(:each){ UserMock.clear_mock }
10
+ before(:each){
11
+ ActiveMocker::Generate.new
12
+ UserMock.clear_mock
13
+ }
8
14
 
9
15
  describe '::column_names' do
10
16
 
11
17
  it 'returns an array of column names found from the schema.rb file' do
12
- expect(UserMock.column_names).to eq(["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin"])
18
+ expect(UserMock.column_names).to eq(["id", "name", "email", "credits", "created_at", "updated_at", "password_digest", "remember_token", "admin"])
13
19
  end
14
20
 
15
21
  end
@@ -23,7 +29,7 @@ describe UserMock do
23
29
  end
24
30
 
25
31
  it 'will raise error if not an attribute or association' do
26
- expect{UserMock.new(baz: "Hello")}.to raise_error('Rejected params: {"baz"=>"Hello"} for UserMock')
32
+ expect{UserMock.new(baz: "Hello")}.to raise_error(ActiveMocker::RejectedParams, '{:baz=>"Hello"} for UserMock')
27
33
  end
28
34
 
29
35
  end
@@ -36,13 +42,14 @@ describe UserMock do
36
42
  end
37
43
 
38
44
  it 'add has_many relationship' do
39
-
40
45
  expect(UserMock.new.microposts.class).to eq ActiveMocker::CollectionAssociation
41
46
  expect(UserMock.new.microposts.count).to eq 0
42
47
  mock_inst = UserMock.new
43
48
  mock_inst.microposts << 1
44
49
  expect(mock_inst.microposts.count).to eq 1
45
-
50
+ mock_inst.microposts << 1
51
+ expect(mock_inst.microposts.count).to eq 2
52
+ expect(mock_inst.microposts.to_a).to eq [1, 1]
46
53
  end
47
54
 
48
55
  end
@@ -1,5 +1,6 @@
1
1
  require 'rspec'
2
2
  $:.unshift File.expand_path('../../', __FILE__)
3
+ require 'active_support/all'
3
4
  require 'active_mocker/collection_association'
4
5
  require 'ostruct'
5
6
 
@@ -13,7 +14,6 @@ describe ActiveMocker::CollectionAssociation do
13
14
  it 'sum values by attribute name' do
14
15
 
15
16
  subject << [OpenStruct.new(value: 1), OpenStruct.new(value: 1)]
16
-
17
17
  expect(subject.sum(:value)).to eq 2
18
18
 
19
19
  end
@@ -25,7 +25,6 @@ describe ActiveMocker::CollectionAssociation do
25
25
  it 'will add a single item to the array' do
26
26
 
27
27
  subject << "item"
28
-
29
28
  expect(subject.count).to eq 1
30
29
  expect(subject.first).to eq 'item'
31
30
 
@@ -34,7 +33,6 @@ describe ActiveMocker::CollectionAssociation do
34
33
  it 'will add a many item to the array' do
35
34
 
36
35
  subject << ['item1', 'item2', 'item3']
37
-
38
36
  expect(subject.count).to eq 3
39
37
  expect(subject).to eq ['item1', 'item2', 'item3']
40
38
 
@@ -74,6 +72,22 @@ describe ActiveMocker::CollectionAssociation do
74
72
 
75
73
  end
76
74
 
75
+ end
76
+
77
+ describe 'each' do
78
+
79
+ it 'works' do
80
+ expect(described_class.new([1,2]).each{|a| a + a}).to eq [1, 2]
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe 'map' do
87
+
88
+ it 'works' do
89
+ expect(described_class.new([1, 2]).map { |a| a + a }).to eq [2, 4]
90
+ end
77
91
 
78
92
  end
79
93
 
@@ -20,18 +20,20 @@ require 'active_support/all'
20
20
  require 'active_hash/ar_api'
21
21
  require 'active_mocker/generate'
22
22
  require 'erb'
23
+ require 'virtus'
24
+ require_relative '../../unit_logger'
23
25
 
24
26
  describe ActiveMocker::Generate do
25
27
 
26
28
  before(:each) do
27
- spec_root = File.expand_path('../../../../', __FILE__)
29
+ app_root = File.expand_path('../../../../', __FILE__)
28
30
  ActiveMocker.config do |config|
29
31
  # Required Options
30
- config.schema_file = File.join(spec_root, 'sample_app_rails_4/db/schema.rb')
31
- config.model_dir = File.join(spec_root, 'sample_app_rails_4/app/models')
32
- config.mock_dir = File.join(spec_root, '/mocks')
33
- # Logging
34
- config.log_level = Logger::WARN #default
32
+ config.schema_file = File.join(app_root, 'sample_app_rails_4/db/schema.rb')
33
+ config.model_dir = File.join(app_root, 'sample_app_rails_4/app/models')
34
+ config.mock_dir = File.join(app_root, 'sample_app_rails_4/spec/mocks')
35
+ config.logger = UnitLogger
36
+
35
37
  end
36
38
 
37
39
  end
@@ -9,9 +9,14 @@ require 'active_mocker/logger'
9
9
  require 'active_mocker/active_record'
10
10
  require 'active_mocker/model_reader'
11
11
  require 'active_mocker/reparameterize'
12
+ require_relative '../../unit_logger'
12
13
 
13
14
  describe ActiveMocker::ModelReader do
14
15
 
16
+ before(:all) do
17
+ ActiveMocker::Logger.set(UnitLogger)
18
+ end
19
+
15
20
  let(:subject){ described_class.new({model_dir: File.expand_path('../../', __FILE__)}).parse('model') }
16
21
 
17
22
  describe '#parse' do
@@ -99,7 +99,7 @@ describe ActiveMocker::SchemaReader do
99
99
 
100
100
  it 'returns all fields from schema' do
101
101
  tables
102
- expect(subject.tables.first.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>[]})
102
+ expect(subject.tables.first.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>{}})
103
103
  end
104
104
 
105
105
  end
@@ -1,205 +1,199 @@
1
- require 'rspec'
2
- $:.unshift File.expand_path('../../../active_mocker', __FILE__)
3
- require 'active_mocker'
4
- require 'string_reader'
5
-
6
- describe 'ReadMe', pending: true do
7
-
8
- before(:each) do
9
- ActiveMocker.configure do |config|
10
- # Required Options
11
- config.schema_file = ""
12
- config.model_dir = ""
13
- config.schema_file_reader = schema_file
14
- config.model_file_reader = model_file
15
- # Additional Options
16
- config.schema_attributes = true #default
17
- config.model_attributes = true #default
18
- config.clear_cache = true
19
- # Logging
20
- config.log_level = Logger::WARN #default
21
- config.migration_dir = '/Users/zeisler/dev/active_mocker/spec/lib/active_mocker/performance/migration'
22
-
23
- end
24
- end
25
-
26
- let(:schema_file){
27
- StringReader.new <<-eos
28
-
29
- ActiveRecord::Schema.define(version: 20140327205359) do
30
-
31
- create_table "people", force: true do |t|
32
- t.integer "account_id"
33
- t.string "first_name", limit: 128
34
- t.string "last_name", limit: 128
35
- t.string "address", limit: 200
36
- t.string "city", limit: 100
37
- end
38
-
39
- end
40
-
41
- eos
42
- }
43
-
44
- let(:model_file){
45
- StringReader.new <<-eos
46
- class Person < ActiveRecord::Base
47
- belongs_to :account
48
-
49
- def bar(name, type=nil)
50
- puts name
51
- end
52
-
53
- def self.bar
54
- end
55
-
56
- end
57
- eos
58
- }
59
-
60
- before do
61
- ActiveMocker.mock('Person')
62
- end
63
-
64
- let(:person_mock){PersonMock}
65
-
66
- describe 'Usage' do
67
-
68
- it 'Mock a Person' do
69
- expect(ActiveMocker.mock('Person')).to eq PersonMock
70
- end
71
-
72
- it '::column_names' do
73
- expect(PersonMock.column_names).to eq ["id", "account_id", "first_name", "last_name", "address", "city"]
74
- end
75
-
76
- it '::new' do
77
- expect(PersonMock.new(first_name: "Dustin", last_name: "Zeisler").inspect).to eq("#<PersonMock id: nil, account_id: nil, first_name: \"Dustin\", last_name: \"Zeisler\", address: nil, city: nil>")
78
- end
79
-
80
- it '#first_name' do
81
- person_mock = PersonMock.new(first_name: "Dustin", last_name: "Zeisler")
82
- expect( person_mock.first_name).to eq 'Dustin'
83
- end
84
-
85
- end
86
-
87
- describe 'When schema.rb changes, the mock fails' do
88
-
89
- let(:schema_file){
90
- StringReader.new <<-eos
91
-
92
- ActiveRecord::Schema.define(version: 20140327205359) do
93
-
94
- create_table "people", force: true do |t|
95
- t.integer "account_id"
96
- t.string "f_name", limit: 128
97
- t.string "l_name", limit: 128
98
- t.string "address", limit: 200
99
- t.string "city", limit: 100
100
- end
101
-
102
- end
103
-
104
- eos
105
- }
106
-
107
- it 'fails' do
108
- expect{ActiveMocker.mock('Person').new(first_name: "Dustin", last_name: "Zeisler")}.to raise_error(RuntimeError)
109
- end
110
-
111
- end
112
-
113
- describe 'Mocking instance and class methods' do
114
-
115
- it 'bar is not Implemented' do
116
- expect{person_mock.bar}.to raise_error( RuntimeError, '::bar is not Implemented for Class: PersonMock' )
117
- end
118
-
119
- it 'is implemented' do
120
- person_mock.mock_instance_method(:bar) do |name, type=nil|
121
- "Now implemented with #{name} and #{type}"
122
- end
123
-
124
- expect(person_mock.new.bar('foo', 'type')).to eq "Now implemented with foo and type"
125
-
126
- person_mock.mock_class_method(:bar) do
127
- "Now implemented"
128
- end
129
-
130
- expect(person_mock.bar).to eq "Now implemented"
131
-
132
- end
133
-
134
- it 'has argument error' do
135
-
136
- person_mock.mock_instance_method(:bar) do |name, type=nil|
137
- "Now implemented with #{name} and #{type}"
138
- end
139
-
140
- expect{person_mock.new.bar}.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1..2)')
141
-
142
- end
143
-
144
- end
145
-
146
- describe 'When the model changes, the mock fails' do
147
-
148
- context 'different arguments' do
149
-
150
- let(:model_file){
151
- StringReader.new <<-eos
152
- class Person < ActiveRecord::Base
153
- belongs_to :account
154
-
155
- def bar(name)
156
- puts name
157
- end
158
-
159
- end
160
- eos
161
- }
162
-
163
- it 'has argument error' do
164
-
165
- person_mock.mock_instance_method(:bar) do |name, type=nil|
166
- "Now implemented with #{name} and #{type}"
167
- end
168
-
169
- expect{person_mock.new.bar('foo', 'type')}.to raise_error(ArgumentError, 'wrong number of arguments (2 for 1)')
170
-
171
- end
172
-
173
- end
174
-
175
- context 'different method name' do
176
-
177
- let(:model_file){
178
- StringReader.new <<-eos
179
- class Person < ActiveRecord::Base
180
- belongs_to :account
181
-
182
- def foo(name)
183
- puts name
184
- end
185
-
186
- end
187
-
188
- eos
189
- }
190
-
191
- it 'when method name changes' do
192
-
193
- person_mock.mock_instance_method(:bar) do |name, type=nil|
194
- "Now implemented with #{name} and #{type}"
195
- end
196
-
197
- expect{person_mock.new.bar}.to raise_error(NoMethodError)
198
-
199
- end
200
-
201
- end
202
-
203
- end
204
-
205
- end
1
+ # require 'rspec'
2
+ # $:.unshift File.expand_path('../../../active_mocker', __FILE__)
3
+ # require 'active_mocker'
4
+ # require 'string_reader'
5
+ #
6
+ # describe 'ReadMe', pending:true do
7
+ #
8
+ # before(:each) do
9
+ # ActiveMocker.configure do |config|
10
+ # # Required Options
11
+ # config.schema_file = ""
12
+ # config.model_dir = ""
13
+ # config.schema_file_reader = schema_file
14
+ # config.model_file_reader = model_file
15
+ # config.mock_dir =
16
+ # config.logger = Logger.new(STDOUT)
17
+ # end
18
+ # end
19
+ #
20
+ # let(:schema_file){
21
+ # StringReader.new <<-eos
22
+ #
23
+ # ActiveRecord::Schema.define(version: 20140327205359) do
24
+ #
25
+ # create_table "people", force: true do |t|
26
+ # t.integer "account_id"
27
+ # t.string "first_name", limit: 128
28
+ # t.string "last_name", limit: 128
29
+ # t.string "address", limit: 200
30
+ # t.string "city", limit: 100
31
+ # end
32
+ #
33
+ # end
34
+ #
35
+ # eos
36
+ # }
37
+ #
38
+ # let(:model_file){
39
+ # StringReader.new <<-eos
40
+ # class Person < ActiveRecord::Base
41
+ # belongs_to :account
42
+ #
43
+ # def bar(name, type=nil)
44
+ # puts name
45
+ # end
46
+ #
47
+ # def self.bar
48
+ # end
49
+ #
50
+ # end
51
+ # eos
52
+ # }
53
+ #
54
+ # before do
55
+ # ActiveMocker.mock('Person')
56
+ # end
57
+ #
58
+ # let(:person_mock){PersonMock}
59
+ #
60
+ # describe 'Usage' do
61
+ #
62
+ # it 'Mock a Person' do
63
+ # expect(ActiveMocker.mock('Person')).to eq PersonMock
64
+ # end
65
+ #
66
+ # it '::column_names' do
67
+ # expect(PersonMock.column_names).to eq ["id", "account_id", "first_name", "last_name", "address", "city"]
68
+ # end
69
+ #
70
+ # it '::new' do
71
+ # expect(PersonMock.new(first_name: "Dustin", last_name: "Zeisler").inspect).to eq("#<PersonMock id: nil, account_id: nil, first_name: \"Dustin\", last_name: \"Zeisler\", address: nil, city: nil>")
72
+ # end
73
+ #
74
+ # it '#first_name' do
75
+ # person_mock = PersonMock.new(first_name: "Dustin", last_name: "Zeisler")
76
+ # expect( person_mock.first_name).to eq 'Dustin'
77
+ # end
78
+ #
79
+ # end
80
+ #
81
+ # describe 'When schema.rb changes, the mock fails' do
82
+ #
83
+ # let(:schema_file){
84
+ # StringReader.new <<-eos
85
+ #
86
+ # ActiveRecord::Schema.define(version: 20140327205359) do
87
+ #
88
+ # create_table "people", force: true do |t|
89
+ # t.integer "account_id"
90
+ # t.string "f_name", limit: 128
91
+ # t.string "l_name", limit: 128
92
+ # t.string "address", limit: 200
93
+ # t.string "city", limit: 100
94
+ # end
95
+ #
96
+ # end
97
+ #
98
+ # eos
99
+ # }
100
+ #
101
+ # it 'fails' do
102
+ # expect{ActiveMocker.mock('Person').new(first_name: "Dustin", last_name: "Zeisler")}.to raise_error(RuntimeError)
103
+ # end
104
+ #
105
+ # end
106
+ #
107
+ # describe 'Mocking instance and class methods' do
108
+ #
109
+ # it 'bar is not Implemented' do
110
+ # expect{person_mock.bar}.to raise_error( RuntimeError, '::bar is not Implemented for Class: PersonMock' )
111
+ # end
112
+ #
113
+ # it 'is implemented' do
114
+ # person_mock.mock_instance_method(:bar) do |name, type=nil|
115
+ # "Now implemented with #{name} and #{type}"
116
+ # end
117
+ #
118
+ # expect(person_mock.new.bar('foo', 'type')).to eq "Now implemented with foo and type"
119
+ #
120
+ # person_mock.mock_class_method(:bar) do
121
+ # "Now implemented"
122
+ # end
123
+ #
124
+ # expect(person_mock.bar).to eq "Now implemented"
125
+ #
126
+ # end
127
+ #
128
+ # it 'has argument error' do
129
+ #
130
+ # person_mock.mock_instance_method(:bar) do |name, type=nil|
131
+ # "Now implemented with #{name} and #{type}"
132
+ # end
133
+ #
134
+ # expect{person_mock.new.bar}.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1..2)')
135
+ #
136
+ # end
137
+ #
138
+ # end
139
+ #
140
+ # describe 'When the model changes, the mock fails' do
141
+ #
142
+ # context 'different arguments' do
143
+ #
144
+ # let(:model_file){
145
+ # StringReader.new <<-eos
146
+ # class Person < ActiveRecord::Base
147
+ # belongs_to :account
148
+ #
149
+ # def bar(name)
150
+ # puts name
151
+ # end
152
+ #
153
+ # end
154
+ # eos
155
+ # }
156
+ #
157
+ # it 'has argument error' do
158
+ #
159
+ # person_mock.mock_instance_method(:bar) do |name, type=nil|
160
+ # "Now implemented with #{name} and #{type}"
161
+ # end
162
+ #
163
+ # expect{person_mock.new.bar('foo', 'type')}.to raise_error(ArgumentError, 'wrong number of arguments (2 for 1)')
164
+ #
165
+ # end
166
+ #
167
+ # end
168
+ #
169
+ # context 'different method name' do
170
+ #
171
+ # let(:model_file){
172
+ # StringReader.new <<-eos
173
+ # class Person < ActiveRecord::Base
174
+ # belongs_to :account
175
+ #
176
+ # def foo(name)
177
+ # puts name
178
+ # end
179
+ #
180
+ # end
181
+ #
182
+ # eos
183
+ # }
184
+ #
185
+ # it 'when method name changes' do
186
+ #
187
+ # person_mock.mock_instance_method(:bar) do |name, type=nil|
188
+ # "Now implemented with #{name} and #{type}"
189
+ # end
190
+ #
191
+ # expect{person_mock.new.bar}.to raise_error(NoMethodError)
192
+ #
193
+ # end
194
+ #
195
+ # end
196
+ #
197
+ # end
198
+ #
199
+ # end