active_mocker 1.2.pre.10 → 1.2.pre.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b4714d7ff79296aea01e1b4f408cded30243ba4f
4
- data.tar.gz: 80a17102f27b2bebb21628e7047856fd91653096
3
+ metadata.gz: af6dc3ef47eac14967fac5cc58c248eaaff7f27b
4
+ data.tar.gz: d5d09b834cb7974ee1f39d002809c9400cbcc654
5
5
  SHA512:
6
- metadata.gz: e3257d142ee5150575d9ca0de79eb6c9bbe74570449867dd8a536d1dd62f0f9eaaceb06530bebb5b43741e150771b7641c7bc3942bdb9433e227cac857b64d32
7
- data.tar.gz: e93ef0984489dc89ff11d52f8bc31e029433be6f6d0f11116a56d5ab77ebef6d0ad53b401f351a403d95143fb8d4d7450b4b49f1116ef7e48e59bf741be8ada1
6
+ metadata.gz: 44910634e7dc665043968e8ee58bc6b83e82e8ae7c4efcc103e328282f4800ef4e89a661c9441c5a4b5f201b86cf0bdaf3690543fd2aef65997b4ea399fcc2a5
7
+ data.tar.gz: b00603af88b32cd5aecbc5e0ece7b030030ae022e107d3ddc7a545ba4623087df6eebdd39778dffe0473899c670f74e1945013450bf9aceb827df83405ed8ca9
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ sample_app_rails_4/log/*
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # ActiveMocker
2
2
  [![Build Status](https://travis-ci.org/zeisler/active_mocker.png?branch=master)](https://travis-ci.org/zeisler/active_mocker)
3
3
 
4
- Create mocks from active record models without loading rails or running a database.
4
+ Create static mocks from active record models without loading rails or running a database.
5
5
 
6
6
  ## Installation
7
7
 
@@ -19,6 +19,7 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
+ Needs updating, for now see the rails sample application
22
23
 
23
24
  #db/schema.rb
24
25
 
data/Rakefile CHANGED
@@ -16,4 +16,5 @@ task :specs do
16
16
  end
17
17
  end
18
18
  raise "Tests Failed" unless system 'rspec'
19
+ raise "Tests Failed" unless system 'rspec sample_app_rails_4/spec'
19
20
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = ActiveMocker::VERSION
9
9
  spec.authors = ["Dustin Zeisler"]
10
10
  spec.email = ["dustin@zive.me"]
11
- spec.summary = %q{Create mocks from active record models without loading rails or running a database}
12
- spec.description = %q{Create mocks from active record models without loading rails or running a database. The Mock's methods have the same arguments as the AR model and if they change you get a error in your test.}
11
+ spec.summary = %q{Create static mocks from active record models without loading rails or running a database.}
12
+ spec.description = %q{Create static mocks from active record models without loading rails or running a database. The Mock's methods have the same arguments as the AR model and if they change you get a error in your test.}
13
13
  spec.homepage = "https://github.com/zeisler/active_mocker"
14
14
  spec.license = "MIT"
15
15
 
@@ -27,9 +27,10 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency "bundler", "~> 1.5"
28
28
  spec.add_development_dependency "rake", "~>10.1"
29
29
  spec.add_development_dependency "rspec", "~>2.14"
30
- # spec.add_development_dependency "i18n", "~>0.6"
31
30
  spec.add_development_dependency "sqlite3", "~>1.3"
32
- spec.add_development_dependency "fuubar"
31
+ spec.add_development_dependency "rails", "~>4.1"
32
+ spec.add_development_dependency "rspec-rails", "~>2"
33
+ spec.add_development_dependency "fuubar", "~>1.3"
33
34
 
34
35
  if ENV['DEBUG'] == '1'
35
36
  spec.add_development_dependency "debase", "~>0.0"
@@ -1,5 +1,5 @@
1
1
  require 'active_mocker/mock_requires'
2
- Object.send(:remove_const, "#{model_name}Mock") if false && ActiveMocker.class_exists?("#{model_name}Mock")
2
+ Object.send(:remove_const, "<%= class_name %>") if ActiveMocker.class_exists?("<%= class_name %>")
3
3
 
4
4
  class <%= class_name %> < ::ActiveHash::Base
5
5
  include ActiveMocker::ActiveHash::ARApi
@@ -10,14 +10,13 @@ class <%= class_name %> < ::ActiveHash::Base
10
10
  <%= attribute_names %>
11
11
  end
12
12
 
13
- def self.association_names
14
- @association_names = <%= association_names.map{|a| a.to_sym} %>
15
- end
16
-
17
13
  def self.attribute_names
18
14
  @attribute_names = <%= attribute_names.map{|a| a.to_sym} %>
19
15
  end
20
16
 
17
+ ##################################
18
+ # Attributes getter/setters #
19
+ ##################################
21
20
  <% attributes.each do |meth| %>
22
21
  def <%= meth %>
23
22
  attributes['<%= meth %>']
@@ -27,7 +26,13 @@ class <%= class_name %> < ::ActiveHash::Base
27
26
  attributes['<%= meth %>'] = val
28
27
  end
29
28
  <% end %>
29
+ ##################################
30
+ # Association getter/setters #
31
+ ##################################
30
32
 
33
+ def self.association_names
34
+ @association_names = <%= association_names.map { |a| a.to_sym } %>
35
+ end
31
36
  <% single_associations.each do |meth| %>
32
37
  def <%= meth %>
33
38
  associations['<%= meth %>']
@@ -37,7 +42,6 @@ class <%= class_name %> < ::ActiveHash::Base
37
42
  associations['<%= meth %>'] = val
38
43
  end
39
44
  <% end %>
40
-
41
45
  <% collection_associations.each do |meth| %>
42
46
  def <%= meth %>
43
47
  associations['<%= meth %>']
@@ -47,20 +51,21 @@ class <%= class_name %> < ::ActiveHash::Base
47
51
  associations['<%= meth %>'] = ActiveMocker::CollectionAssociation.new(val)
48
52
  end
49
53
  <% end %>
54
+ ##################################
55
+ # Model Methods getter/setters #
56
+ ##################################
50
57
 
51
58
  def self.model_instance_methods
52
59
  return @model_instance_methods if @model_instance_methods
53
60
  @model_instance_methods = {}<% model_instance_methods.each do |key, value| %>
54
- @model_instance_methods[:<%= key %>] = :not_implemented
55
- <% end %>
61
+ @model_instance_methods[:<%= key %>] = :not_implemented<% end %>
56
62
  @model_instance_methods
57
63
  end
58
64
 
59
65
  def self.model_class_methods
60
66
  return @model_class_methods if @model_class_methods
61
67
  @model_class_methods = {}<% model_class_methods.each do |key, value| %>
62
- @model_class_methods[:<%= key %>] = :not_implemented
63
- <% end %>
68
+ @model_class_methods[:<%= key %>] = :not_implemented<% end %>
64
69
  @model_class_methods
65
70
  end
66
71
 
@@ -71,7 +76,6 @@ class <%= class_name %> < ::ActiveHash::Base
71
76
  instance_exec(*[<%= method.params_pass %>], &block)
72
77
  end
73
78
  <% end %>
74
-
75
79
  <% class_methods.each do |method| %>
76
80
  def self.<%= method.method %>(<%= method.params %>)
77
81
  block = model_class_methods[<%= method.method.inspect %>]
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "1.2.pre.10"
2
+ VERSION = "1.2.pre.11"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_mocker/mock_requires'
2
- Object.send(:remove_const, "#{model_name}Mock") if false && ActiveMocker.class_exists?("#{model_name}Mock")
2
+ Object.send(:remove_const, "MicropostMock") if ActiveMocker.class_exists?("MicropostMock")
3
3
 
4
4
  class MicropostMock < ::ActiveHash::Base
5
5
  include ActiveMocker::ActiveHash::ARApi
@@ -10,14 +10,13 @@ class MicropostMock < ::ActiveHash::Base
10
10
  ["id", "content", "user_id", "created_at", "updated_at"]
11
11
  end
12
12
 
13
- def self.association_names
14
- @association_names = [:user]
15
- end
16
-
17
13
  def self.attribute_names
18
14
  @attribute_names = [:id, :content, :user_id, :created_at, :updated_at]
19
15
  end
20
16
 
17
+ ##################################
18
+ # Attributes getter/setters #
19
+ ##################################
21
20
 
22
21
  def id
23
22
  attributes['id']
@@ -59,7 +58,13 @@ class MicropostMock < ::ActiveHash::Base
59
58
  attributes['updated_at'] = val
60
59
  end
61
60
 
61
+ ##################################
62
+ # Association getter/setters #
63
+ ##################################
62
64
 
65
+ def self.association_names
66
+ @association_names = [:user]
67
+ end
63
68
 
64
69
  def user
65
70
  associations['user']
@@ -70,7 +75,9 @@ class MicropostMock < ::ActiveHash::Base
70
75
  end
71
76
 
72
77
 
73
-
78
+ ##################################
79
+ # Model Methods getter/setters #
80
+ ##################################
74
81
 
75
82
  def self.model_instance_methods
76
83
  return @model_instance_methods if @model_instance_methods
@@ -82,13 +89,11 @@ class MicropostMock < ::ActiveHash::Base
82
89
  return @model_class_methods if @model_class_methods
83
90
  @model_class_methods = {}
84
91
  @model_class_methods[:from_users_followed_by] = :not_implemented
85
-
86
92
  @model_class_methods
87
93
  end
88
94
 
89
95
 
90
96
 
91
-
92
97
  def self.from_users_followed_by(user)
93
98
  block = model_class_methods[:from_users_followed_by]
94
99
  is_implemented(block, "::from_users_followed_by")
@@ -1,5 +1,5 @@
1
1
  require 'active_mocker/mock_requires'
2
- Object.send(:remove_const, "#{model_name}Mock") if false && ActiveMocker.class_exists?("#{model_name}Mock")
2
+ Object.send(:remove_const, "RelationshipMock") if ActiveMocker.class_exists?("RelationshipMock")
3
3
 
4
4
  class RelationshipMock < ::ActiveHash::Base
5
5
  include ActiveMocker::ActiveHash::ARApi
@@ -10,14 +10,13 @@ class RelationshipMock < ::ActiveHash::Base
10
10
  ["id", "follower_id", "followed_id", "created_at", "updated_at"]
11
11
  end
12
12
 
13
- def self.association_names
14
- @association_names = [:follower, :followed]
15
- end
16
-
17
13
  def self.attribute_names
18
14
  @attribute_names = [:id, :follower_id, :followed_id, :created_at, :updated_at]
19
15
  end
20
16
 
17
+ ##################################
18
+ # Attributes getter/setters #
19
+ ##################################
21
20
 
22
21
  def id
23
22
  attributes['id']
@@ -59,7 +58,13 @@ class RelationshipMock < ::ActiveHash::Base
59
58
  attributes['updated_at'] = val
60
59
  end
61
60
 
61
+ ##################################
62
+ # Association getter/setters #
63
+ ##################################
62
64
 
65
+ def self.association_names
66
+ @association_names = [:follower, :followed]
67
+ end
63
68
 
64
69
  def follower
65
70
  associations['follower']
@@ -78,7 +83,9 @@ class RelationshipMock < ::ActiveHash::Base
78
83
  end
79
84
 
80
85
 
81
-
86
+ ##################################
87
+ # Model Methods getter/setters #
88
+ ##################################
82
89
 
83
90
  def self.model_instance_methods
84
91
  return @model_instance_methods if @model_instance_methods
@@ -95,5 +102,4 @@ class RelationshipMock < ::ActiveHash::Base
95
102
 
96
103
 
97
104
 
98
-
99
105
  end
data/mocks/user_mock.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'active_mocker/mock_requires'
2
- Object.send(:remove_const, "#{model_name}Mock") if false && ActiveMocker.class_exists?("#{model_name}Mock")
2
+ Object.send(:remove_const, "UserMock") if ActiveMocker.class_exists?("UserMock")
3
3
 
4
4
  class UserMock < ::ActiveHash::Base
5
5
  include ActiveMocker::ActiveHash::ARApi
@@ -10,14 +10,13 @@ class UserMock < ::ActiveHash::Base
10
10
  ["id", "name", "email", "created_at", "updated_at", "password_digest", "remember_token", "admin"]
11
11
  end
12
12
 
13
- def self.association_names
14
- @association_names = [:microposts, :relationships, :followed_users, :reverse_relationships, :followers]
15
- end
16
-
17
13
  def self.attribute_names
18
14
  @attribute_names = [:id, :name, :email, :created_at, :updated_at, :password_digest, :remember_token, :admin]
19
15
  end
20
16
 
17
+ ##################################
18
+ # Attributes getter/setters #
19
+ ##################################
21
20
 
22
21
  def id
23
22
  attributes['id']
@@ -83,8 +82,13 @@ class UserMock < ::ActiveHash::Base
83
82
  attributes['admin'] = val
84
83
  end
85
84
 
85
+ ##################################
86
+ # Association getter/setters #
87
+ ##################################
86
88
 
87
-
89
+ def self.association_names
90
+ @association_names = [:microposts, :relationships, :followed_users, :reverse_relationships, :followers]
91
+ end
88
92
 
89
93
 
90
94
  def microposts
@@ -127,18 +131,17 @@ class UserMock < ::ActiveHash::Base
127
131
  associations['followers'] = ActiveMocker::CollectionAssociation.new(val)
128
132
  end
129
133
 
134
+ ##################################
135
+ # Model Methods getter/setters #
136
+ ##################################
130
137
 
131
138
  def self.model_instance_methods
132
139
  return @model_instance_methods if @model_instance_methods
133
140
  @model_instance_methods = {}
134
- @model_instance_methods[:feed] = :not_implemented
135
-
136
- @model_instance_methods[:following?] = :not_implemented
137
-
138
- @model_instance_methods[:follow!] = :not_implemented
139
-
140
- @model_instance_methods[:unfollow!] = :not_implemented
141
-
141
+ @model_instance_methods[:feed] = :not_implemented
142
+ @model_instance_methods[:following?] = :not_implemented
143
+ @model_instance_methods[:follow!] = :not_implemented
144
+ @model_instance_methods[:unfollow!] = :not_implemented
142
145
  @model_instance_methods
143
146
  end
144
147
 
@@ -146,9 +149,7 @@ class UserMock < ::ActiveHash::Base
146
149
  return @model_class_methods if @model_class_methods
147
150
  @model_class_methods = {}
148
151
  @model_class_methods[:new_remember_token] = :not_implemented
149
-
150
152
  @model_class_methods[:digest] = :not_implemented
151
-
152
153
  @model_class_methods
153
154
  end
154
155
 
@@ -178,7 +179,6 @@ class UserMock < ::ActiveHash::Base
178
179
  end
179
180
 
180
181
 
181
-
182
182
  def self.new_remember_token()
183
183
  block = model_class_methods[:new_remember_token]
184
184
  is_implemented(block, "::new_remember_token")
@@ -1,53 +1,12 @@
1
1
  source 'https://rubygems.org'
2
- #ruby-gemset=railstutorial_rails_4_0
3
2
 
4
3
  gem 'rails', '4.1'
5
- gem 'bootstrap-sass', '2.3.2.0'
6
- gem 'sprockets', '2.11.0'
7
- gem 'bcrypt-ruby', '3.1.2'
8
- gem 'faker', '1.1.2'
9
- gem 'will_paginate', '3.0.4'
10
- gem 'bootstrap-will_paginate', '0.0.9'
11
4
 
12
5
  group :development, :test do
13
- gem 'sqlite3', '1.3.8'
6
+ gem 'sqlite3', '~>1.3'
14
7
  gem 'rspec-rails', '~>2'
15
8
  end
16
9
 
17
10
  group :test do
18
- gem 'active_mocker', :path => "../../../active_mocker"
19
- gem 'selenium-webdriver', '2.35.1'
20
- gem 'capybara', '2.1.0'
21
- gem 'factory_girl_rails', '4.2.0'
22
- gem 'cucumber-rails', '1.3.0', :require => false
23
- gem 'database_cleaner', github: 'bmabey/database_cleaner'
24
- gem 'fuubar'
25
- gem "minitest"
26
-
27
- # Uncomment this line on OS X.
28
- gem 'growl', '1.0.3'
29
-
30
- # Uncomment these lines on Linux.
31
- # gem 'libnotify', '0.8.0'
32
-
33
- # Uncomment these lines on Windows.
34
- # gem 'rb-notifu', '0.0.4'
35
- # gem 'win32console', '1.3.2'
36
- # gem 'wdm', '0.1.0'
37
- end
38
-
39
- gem 'sass-rails', '4.0.1'
40
- gem 'uglifier', '2.1.1'
41
- gem 'coffee-rails', '4.0.1'
42
- gem 'jquery-rails', '3.0.4'
43
- gem 'turbolinks', '1.1.1'
44
- gem 'jbuilder', '1.0.2'
45
-
46
- group :doc do
47
- gem 'sdoc', '0.3.20', require: false
48
- end
49
-
50
- group :production do
51
- gem 'pg', '0.15.1'
52
- gem 'rails_12factor', '0.0.2'
11
+ gem 'active_mocker', :path => "../../active_mocker"
53
12
  end
@@ -6,14 +6,6 @@ class User < ActiveRecord::Base
6
6
  class_name: "Relationship",
7
7
  dependent: :destroy
8
8
  has_many :followers, through: :reverse_relationships, source: :follower
9
- before_save { self.email = email.downcase }
10
- before_create :create_remember_token
11
- validates :name, presence: true, length: { maximum: 50 }
12
- VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
13
- validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
14
- uniqueness: { case_sensitive: false }
15
- has_secure_password
16
- validates :password, length: { minimum: 6 }
17
9
 
18
10
  def User.new_remember_token
19
11
  SecureRandom.urlsafe_base64
@@ -4,7 +4,7 @@ ActiveMocker::Generate.configure do |config|
4
4
  # Required Options
5
5
  config.schema_file = File.join(Rails.root, 'db/schema.rb')
6
6
  config.model_dir = File.join(Rails.root, 'app/models')
7
- config.mock_dir = File.join(Rails.root, 'specs/mocks')
7
+ config.mock_dir = File.join(Rails.root, 'spec/mocks')
8
8
  # Logging
9
9
  config.log_level = Logger::WARN #default
10
10
  end
@@ -0,0 +1,116 @@
1
+ $:.unshift File.expand_path('../', __FILE__)
2
+ require 'spec_helper'
3
+ load 'mocks/user_mock.rb'
4
+ load 'mocks/micropost_mock.rb'
5
+
6
+ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
7
+
8
+ after(:each) do
9
+ UserMock.destroy_all
10
+ User.destroy_all
11
+ MicropostMock.destroy_all
12
+ Micropost.destroy_all
13
+ end
14
+
15
+ let(:attributes) { {name: 'Dustin Zeisler', email: 'dustin@example.com'} }
16
+ let(:attributes_with_admin) { {name: 'Dustin Zeisler', email: 'dustin@example.com', admin: true} }
17
+
18
+ describe '::superclass' do
19
+
20
+ it 'mock has super of active hash' do
21
+ expect(UserMock.superclass.name).to eq "ActiveHash::Base"
22
+ end
23
+
24
+ it 'ar has super of ar' do
25
+ expect(User.superclass.name).to eq "ActiveRecord::Base"
26
+ end
27
+
28
+ end
29
+
30
+ describe '::create' do
31
+
32
+ let(:create_attributes){attributes}
33
+
34
+ it 'mock will take all attributes that AR takes' do
35
+ User.create(create_attributes)
36
+ UserMock.create(create_attributes)
37
+ end
38
+
39
+ end
40
+
41
+ describe '#attributes' do
42
+
43
+ let(:user_ar){User.new(attributes)}
44
+ let(:user_mock){UserMock.new(attributes)}
45
+
46
+ it 'they are the same' do
47
+ expect(user_mock.attributes).to eq user_ar.attributes
48
+ end
49
+
50
+ end
51
+
52
+ describe 'associations' do
53
+
54
+ let(:micropost){ Micropost.create(content: 'post')}
55
+ let(:create_attributes){attributes.merge({microposts: [micropost]})}
56
+
57
+ let(:user_ar){User.new(create_attributes)}
58
+ let(:user_mock){UserMock.new(create_attributes)}
59
+
60
+ it 'the Mock when adding an association will not set the _id attribute, do it manually' do
61
+ expect(user_mock.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => nil, "admin" => nil})
62
+ expect(user_mock.microposts).to eq [micropost]
63
+ end
64
+
65
+ it 'Ar will not include associations in attributes' do
66
+ expect(user_ar.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => nil, "admin" => nil})
67
+ expect(user_ar.microposts).to eq [micropost]
68
+ end
69
+
70
+ end
71
+
72
+ describe 'column_names' do
73
+
74
+ it 'they are the same' do
75
+ expect(UserMock.column_names).to eq User.column_names
76
+ end
77
+
78
+ end
79
+
80
+ describe '::find_by' do
81
+
82
+ let!(:ar_record){User.create(attributes)}
83
+ let!(:mock_record){UserMock.create(attributes)}
84
+
85
+ it 'AR' do
86
+ expect(ar_record).to eq User.find_by(attributes)
87
+ end
88
+
89
+ it 'Mock' do
90
+ expect(UserMock.create(attributes_with_admin)).to eq UserMock.find_by(attributes_with_admin)
91
+ end
92
+
93
+ end
94
+
95
+ describe '::where' do
96
+
97
+ let(:ar_record){User.create(attributes)}
98
+ let(:mock_record){UserMock.create(attributes)}
99
+ let(:mock_record_2){UserMock.create(attributes_with_admin)}
100
+
101
+ it 'AR' do
102
+ expect([ar_record]).to eq User.where(attributes)
103
+ end
104
+
105
+ it 'Mock' do
106
+ expect([mock_record]).to eq UserMock.where(attributes)
107
+ end
108
+
109
+ it 'Mock will not take sql string needs to be mocked' do
110
+ UserMock.create(attributes_with_admin)
111
+ expect{UserMock.where("name = 'Dustin Zeisler'")}.to raise_error
112
+ end
113
+
114
+ end
115
+
116
+ end