active_mocker 1.1.23 → 1.2.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -22
- data/active_mocker.gemspec +1 -0
- data/lib/active_hash/init.rb +1 -1
- data/lib/active_mocker.rb +4 -5
- data/lib/active_mocker/active_record/schema.rb +2 -4
- data/lib/active_mocker/config.rb +6 -22
- data/lib/active_mocker/field.rb +3 -1
- data/lib/active_mocker/generate.rb +161 -0
- data/lib/active_mocker/mock_class_methods.rb +82 -0
- data/lib/active_mocker/mock_instance_methods.rb +81 -0
- data/lib/active_mocker/mock_requires.rb +12 -0
- data/lib/active_mocker/mock_task.rb +12 -0
- data/lib/active_mocker/mock_template.erb +84 -0
- data/lib/active_mocker/public_methods.rb +6 -2
- data/lib/active_mocker/schema_reader.rb +35 -16
- data/lib/active_mocker/table.rb +2 -0
- data/lib/active_mocker/version.rb +1 -1
- data/mocks/micropost_mock.rb +100 -0
- data/mocks/relationship_mock.rb +100 -0
- data/mocks/user_mock.rb +196 -0
- data/plan_mock.rb +2323 -0
- data/spec/lib/active_mocker/generate_spec.rb +49 -0
- data/spec/lib/active_mocker/{base_spec.rb → performance/base_spec.rb} +17 -37
- data/spec/lib/active_mocker/performance/large_schema.rb +3576 -0
- data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
- data/spec/lib/active_mocker/performance/schema_reader_spec.rb +96 -0
- data/spec/lib/active_mocker/schema_reader_spec.rb +12 -27
- data/spec/lib/compare_mocker_and_record_spec.rb +3 -2
- data/spec/lib/readme_spec.rb +205 -0
- data/spec/mocks/micropost_mock.rb +100 -0
- data/spec/mocks/relationship_mock.rb +100 -0
- data/spec/mocks/user_mock.rb +196 -0
- data/spec/mocks/user_mock_spec.rb +173 -0
- metadata +48 -9
- data/lib/active_mocker/base.rb +0 -356
- data/spec/lib/active_mocker/active_record/schema_spec.rb +0 -2721
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'active_mocker/mock_requires'
|
2
|
+
|
3
|
+
Object.send(:remove_const, 'RelationshipMock') if class_exists? 'RelationshipMock'
|
4
|
+
|
5
|
+
class RelationshipMock < ::ActiveHash::Base
|
6
|
+
include ActiveMocker::ActiveHash::ARApi
|
7
|
+
include ActiveMocker::MockInstanceMethods
|
8
|
+
extend ActiveMocker::MockClassMethods
|
9
|
+
|
10
|
+
def self.column_names
|
11
|
+
["id", "follower_id", "followed_id", "created_at", "updated_at"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.association_names
|
15
|
+
@association_names = [:follower, :followed]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.attribute_names
|
19
|
+
@attribute_names = [:id, :follower_id, :followed_id, :created_at, :updated_at]
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def id
|
24
|
+
attributes['id']
|
25
|
+
end
|
26
|
+
|
27
|
+
def id=(val)
|
28
|
+
attributes['id'] = val
|
29
|
+
end
|
30
|
+
|
31
|
+
def follower_id
|
32
|
+
attributes['follower_id']
|
33
|
+
end
|
34
|
+
|
35
|
+
def follower_id=(val)
|
36
|
+
attributes['follower_id'] = val
|
37
|
+
end
|
38
|
+
|
39
|
+
def followed_id
|
40
|
+
attributes['followed_id']
|
41
|
+
end
|
42
|
+
|
43
|
+
def followed_id=(val)
|
44
|
+
attributes['followed_id'] = val
|
45
|
+
end
|
46
|
+
|
47
|
+
def created_at
|
48
|
+
attributes['created_at']
|
49
|
+
end
|
50
|
+
|
51
|
+
def created_at=(val)
|
52
|
+
attributes['created_at'] = val
|
53
|
+
end
|
54
|
+
|
55
|
+
def updated_at
|
56
|
+
attributes['updated_at']
|
57
|
+
end
|
58
|
+
|
59
|
+
def updated_at=(val)
|
60
|
+
attributes['updated_at'] = val
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def follower
|
66
|
+
associations['follower']
|
67
|
+
end
|
68
|
+
|
69
|
+
def follower=(val)
|
70
|
+
associations['follower'] = val
|
71
|
+
end
|
72
|
+
|
73
|
+
def followed
|
74
|
+
associations['followed']
|
75
|
+
end
|
76
|
+
|
77
|
+
def followed=(val)
|
78
|
+
associations['followed'] = val
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
def self.model_instance_methods
|
85
|
+
return @model_instance_methods if @model_instance_methods
|
86
|
+
@model_instance_methods = {}
|
87
|
+
@model_instance_methods
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.model_class_methods
|
91
|
+
return @model_class_methods if @model_class_methods
|
92
|
+
@model_class_methods = {}
|
93
|
+
@model_class_methods
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require 'active_mocker/mock_requires'
|
2
|
+
|
3
|
+
Object.send(:remove_const, 'UserMock') if class_exists? 'UserMock'
|
4
|
+
|
5
|
+
class UserMock < ::ActiveHash::Base
|
6
|
+
include ActiveMocker::ActiveHash::ARApi
|
7
|
+
include ActiveMocker::MockInstanceMethods
|
8
|
+
extend ActiveMocker::MockClassMethods
|
9
|
+
|
10
|
+
def self.column_names
|
11
|
+
["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.association_names
|
15
|
+
@association_names = [:microposts, :relationships, :followed_users, :reverse_relationships, :followers]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.attribute_names
|
19
|
+
@attribute_names = [:id, :name, :email, :created_at, :updated_at, :password_digest, :remember_token, :admin]
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
def id
|
24
|
+
attributes['id']
|
25
|
+
end
|
26
|
+
|
27
|
+
def id=(val)
|
28
|
+
attributes['id'] = val
|
29
|
+
end
|
30
|
+
|
31
|
+
def name
|
32
|
+
attributes['name']
|
33
|
+
end
|
34
|
+
|
35
|
+
def name=(val)
|
36
|
+
attributes['name'] = val
|
37
|
+
end
|
38
|
+
|
39
|
+
def email
|
40
|
+
attributes['email']
|
41
|
+
end
|
42
|
+
|
43
|
+
def email=(val)
|
44
|
+
attributes['email'] = val
|
45
|
+
end
|
46
|
+
|
47
|
+
def created_at
|
48
|
+
attributes['created_at']
|
49
|
+
end
|
50
|
+
|
51
|
+
def created_at=(val)
|
52
|
+
attributes['created_at'] = val
|
53
|
+
end
|
54
|
+
|
55
|
+
def updated_at
|
56
|
+
attributes['updated_at']
|
57
|
+
end
|
58
|
+
|
59
|
+
def updated_at=(val)
|
60
|
+
attributes['updated_at'] = val
|
61
|
+
end
|
62
|
+
|
63
|
+
def password_digest
|
64
|
+
attributes['password_digest']
|
65
|
+
end
|
66
|
+
|
67
|
+
def password_digest=(val)
|
68
|
+
attributes['password_digest'] = val
|
69
|
+
end
|
70
|
+
|
71
|
+
def remember_token
|
72
|
+
attributes['remember_token']
|
73
|
+
end
|
74
|
+
|
75
|
+
def remember_token=(val)
|
76
|
+
attributes['remember_token'] = val
|
77
|
+
end
|
78
|
+
|
79
|
+
def admin
|
80
|
+
attributes['admin']
|
81
|
+
end
|
82
|
+
|
83
|
+
def admin=(val)
|
84
|
+
attributes['admin'] = val
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def microposts
|
92
|
+
associations['microposts']
|
93
|
+
end
|
94
|
+
|
95
|
+
def microposts=(val)
|
96
|
+
associations['microposts'] = ActiveMocker::CollectionAssociation.new(val)
|
97
|
+
end
|
98
|
+
|
99
|
+
def relationships
|
100
|
+
associations['relationships']
|
101
|
+
end
|
102
|
+
|
103
|
+
def relationships=(val)
|
104
|
+
associations['relationships'] = ActiveMocker::CollectionAssociation.new(val)
|
105
|
+
end
|
106
|
+
|
107
|
+
def followed_users
|
108
|
+
associations['followed_users']
|
109
|
+
end
|
110
|
+
|
111
|
+
def followed_users=(val)
|
112
|
+
associations['followed_users'] = ActiveMocker::CollectionAssociation.new(val)
|
113
|
+
end
|
114
|
+
|
115
|
+
def reverse_relationships
|
116
|
+
associations['reverse_relationships']
|
117
|
+
end
|
118
|
+
|
119
|
+
def reverse_relationships=(val)
|
120
|
+
associations['reverse_relationships'] = ActiveMocker::CollectionAssociation.new(val)
|
121
|
+
end
|
122
|
+
|
123
|
+
def followers
|
124
|
+
associations['followers']
|
125
|
+
end
|
126
|
+
|
127
|
+
def followers=(val)
|
128
|
+
associations['followers'] = ActiveMocker::CollectionAssociation.new(val)
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
def self.model_instance_methods
|
133
|
+
return @model_instance_methods if @model_instance_methods
|
134
|
+
@model_instance_methods = {}
|
135
|
+
@model_instance_methods[:feed] = :not_implemented
|
136
|
+
|
137
|
+
@model_instance_methods[:following?] = :not_implemented
|
138
|
+
|
139
|
+
@model_instance_methods[:follow!] = :not_implemented
|
140
|
+
|
141
|
+
@model_instance_methods[:unfollow!] = :not_implemented
|
142
|
+
|
143
|
+
@model_instance_methods
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.model_class_methods
|
147
|
+
return @model_class_methods if @model_class_methods
|
148
|
+
@model_class_methods = {}
|
149
|
+
@model_class_methods[:new_remember_token] = :not_implemented
|
150
|
+
|
151
|
+
@model_class_methods[:digest] = :not_implemented
|
152
|
+
|
153
|
+
@model_class_methods
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
def feed()
|
158
|
+
block = model_instance_methods[:feed]
|
159
|
+
self.class.is_implemented(block, "#feed")
|
160
|
+
instance_exec(*[], &block)
|
161
|
+
end
|
162
|
+
|
163
|
+
def following?(other_user)
|
164
|
+
block = model_instance_methods[:following?]
|
165
|
+
self.class.is_implemented(block, "#following?")
|
166
|
+
instance_exec(*[other_user], &block)
|
167
|
+
end
|
168
|
+
|
169
|
+
def follow!(other_user)
|
170
|
+
block = model_instance_methods[:follow!]
|
171
|
+
self.class.is_implemented(block, "#follow!")
|
172
|
+
instance_exec(*[other_user], &block)
|
173
|
+
end
|
174
|
+
|
175
|
+
def unfollow!(other_user)
|
176
|
+
block = model_instance_methods[:unfollow!]
|
177
|
+
self.class.is_implemented(block, "#unfollow!")
|
178
|
+
instance_exec(*[other_user], &block)
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
def self.new_remember_token()
|
184
|
+
block = model_class_methods[:new_remember_token]
|
185
|
+
is_implemented(block, "::new_remember_token")
|
186
|
+
instance_exec(*[], &block)
|
187
|
+
end
|
188
|
+
|
189
|
+
def self.digest(token)
|
190
|
+
block = model_class_methods[:digest]
|
191
|
+
is_implemented(block, "::digest")
|
192
|
+
instance_exec(*[token], &block)
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
$:.unshift File.expand_path('../', __FILE__)
|
3
|
+
load 'spec/mocks/user_mock.rb'
|
4
|
+
|
5
|
+
describe UserMock do
|
6
|
+
|
7
|
+
let(:mock_class){described_class}
|
8
|
+
|
9
|
+
describe '::column_names' do
|
10
|
+
|
11
|
+
it 'returns an array of column names found from the schema.rb file' do
|
12
|
+
expect(mock_class.column_names).to eq(["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin"])
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'mass_assignment' do
|
18
|
+
|
19
|
+
it "can pass any or all attributes from schema in initializer" do
|
20
|
+
result = mock_class.new(name: "Sam", email: 'Walton')
|
21
|
+
expect(result.name).to eq 'Sam'
|
22
|
+
expect(result.email).to eq 'Walton'
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'will raise error if not an attribute or association' do
|
26
|
+
expect{mock_class.new(baz: "Hello")}.to raise_error('Rejected params: {"baz"=>"Hello"} for UserMock')
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'relationships' do
|
32
|
+
|
33
|
+
it 'add instance methods from model relationships' do
|
34
|
+
result = mock_class.new(followers: [1])
|
35
|
+
expect(result.followers).to eq [1]
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'add has_many relationship' do
|
39
|
+
|
40
|
+
expect(mock_class.new.microposts.class).to eq ActiveMocker::CollectionAssociation
|
41
|
+
expect(mock_class.new.microposts.count).to eq 0
|
42
|
+
mock_inst = mock_class.new
|
43
|
+
mock_inst.microposts << 1
|
44
|
+
expect(mock_inst.microposts.count).to eq 1
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'instance methods' do
|
51
|
+
|
52
|
+
it 'will raise exception for unimplemented methods' do
|
53
|
+
expect(mock_class.new.method(:following?).parameters).to eq [[:req, :other_user]]
|
54
|
+
expect{mock_class.new.following?}.to raise_error ArgumentError
|
55
|
+
expect{mock_class.new.following?('foo')}.to raise_error(RuntimeError, '#following? is not Implemented for Class: UserMock')
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can be implemented dynamically' do
|
59
|
+
|
60
|
+
mock_class.mock_instance_method(:follow!) do |other_user|
|
61
|
+
"Now implemented with #{other_user}"
|
62
|
+
end
|
63
|
+
result = mock_class.new
|
64
|
+
result = result.follow!('foo')
|
65
|
+
expect(result).to eq "Now implemented with foo"
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'can reference another mock' do
|
70
|
+
|
71
|
+
mock_class.mock_instance_method(:following?) do |person|
|
72
|
+
true
|
73
|
+
end
|
74
|
+
|
75
|
+
mock_class.mock_instance_method(:follow!) do |person|
|
76
|
+
following?(person)
|
77
|
+
end
|
78
|
+
|
79
|
+
expect(mock_class.new.follow!("name")).to eq true
|
80
|
+
expect(mock_class.new.following?(1)).to eq true
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'class methods' do
|
86
|
+
|
87
|
+
it 'will raise exception for unimplemented methods' do
|
88
|
+
expect{mock_class.new_remember_token}.to raise_error('::new_remember_token is not Implemented for Class: UserMock')
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'can be implemented as follows' do
|
92
|
+
|
93
|
+
mock_class.mock_class_method(:new_remember_token) do
|
94
|
+
"Now implemented"
|
95
|
+
end
|
96
|
+
expect{mock_class.new_remember_token}.to_not raise_error
|
97
|
+
expect(mock_class.new_remember_token).to eq("Now implemented")
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'active_hash' do
|
104
|
+
|
105
|
+
it 'uses active_hash::base as superclass' do
|
106
|
+
expect(mock_class.superclass.name).to eq 'ActiveHash::Base'
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'can save to class and then find instance by attribute' do
|
110
|
+
record = mock_class.create(name: "Sam")
|
111
|
+
expect(mock_class.find_by(name:"Sam")).to eq record
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
it '#update' do
|
116
|
+
|
117
|
+
person = mock_class.create(name: 'Justin')
|
118
|
+
|
119
|
+
expect(UserMock.first.name).to eq 'Justin'
|
120
|
+
person.update(name: 'Dustin')
|
121
|
+
expect(UserMock.first.name).to eq 'Dustin'
|
122
|
+
|
123
|
+
expect(person.name).to eq 'Dustin'
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
it '::destroy_all' do
|
128
|
+
|
129
|
+
mock_class.create
|
130
|
+
|
131
|
+
expect(mock_class.count).to eq 1
|
132
|
+
|
133
|
+
mock_class.destroy_all
|
134
|
+
|
135
|
+
expect(mock_class.count).to eq 0
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
it '::find_by' do
|
140
|
+
person = mock_class.create(name: 'dustin')
|
141
|
+
expect(mock_class.find_by(name: 'dustin')).to eq person
|
142
|
+
end
|
143
|
+
|
144
|
+
it '::find_or_create_by' do
|
145
|
+
person = mock_class.find_or_create_by(name: 'dustin')
|
146
|
+
expect(mock_class.find_by(name: 'dustin')).to eq person
|
147
|
+
person = mock_class.find_or_create_by(name: 'dustin')
|
148
|
+
expect(mock_class.count).to eq 1
|
149
|
+
end
|
150
|
+
|
151
|
+
it '::find_or_create_by with update' do
|
152
|
+
mock_class.create(name: 'dustin')
|
153
|
+
person = mock_class.find_or_create_by(name: 'dustin')
|
154
|
+
person.update(email: 'Zeisler')
|
155
|
+
expect(mock_class.first.attributes).to eq person.attributes
|
156
|
+
expect(mock_class.count).to eq 1
|
157
|
+
end
|
158
|
+
|
159
|
+
it '::find_or_initialize_by' do
|
160
|
+
person = mock_class.find_or_initialize_by(name: 'dustin')
|
161
|
+
expect(person.persisted?).to eq false
|
162
|
+
mock_class.create(name: 'dustin')
|
163
|
+
person = mock_class.find_or_initialize_by(name: 'dustin')
|
164
|
+
expect(person.persisted?).to eq true
|
165
|
+
end
|
166
|
+
|
167
|
+
after(:each) do
|
168
|
+
mock_class.delete_all
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|