active_mocker 1.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +61 -25
- data/Rakefile +1 -1
- data/active_mocker.gemspec +3 -3
- data/lib/active_hash/init.rb +13 -22
- data/lib/active_mocker.rb +1 -0
- data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
- data/lib/active_mocker/active_record/unknown_module.rb +3 -3
- data/lib/active_mocker/collection_association.rb +19 -20
- data/lib/active_mocker/config.rb +7 -4
- data/lib/active_mocker/field.rb +4 -4
- data/lib/active_mocker/generate.rb +75 -18
- data/lib/active_mocker/logger.rb +10 -1
- data/lib/active_mocker/mock_class_methods.rb +12 -2
- data/lib/active_mocker/mock_instance_methods.rb +3 -2
- data/lib/active_mocker/mock_requires.rb +2 -1
- data/lib/active_mocker/mock_template.erb +25 -22
- data/lib/active_mocker/public_methods.rb +6 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/app/models/micropost.rb +0 -2
- data/sample_app_rails_4/bin/rspec +16 -0
- data/sample_app_rails_4/config/application.rb +0 -3
- data/sample_app_rails_4/config/database.yml +5 -6
- data/sample_app_rails_4/config/environments/development.rb +0 -2
- data/sample_app_rails_4/config/environments/test.rb +0 -1
- data/sample_app_rails_4/config/initializers/active_mocker.rb +7 -5
- data/sample_app_rails_4/db/migrate/20130311191400_create_users.rb +1 -1
- data/sample_app_rails_4/db/migrate/20130315015932_add_admin_to_users.rb +1 -1
- data/sample_app_rails_4/db/schema.rb +4 -3
- data/sample_app_rails_4/lib/tasks/{mocks.rake → active_mocker.rake} +5 -5
- data/sample_app_rails_4/lib/unit_logger.rb +22 -0
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +110 -4
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +31 -27
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +29 -24
- data/sample_app_rails_4/spec/mocks/user_mock.rb +69 -58
- data/sample_app_rails_4/spec/spec_helper.rb +6 -7
- data/sample_app_rails_4/spec/user_mock_spec.rb +14 -7
- data/spec/lib/active_mocker/collection_association_spec.rb +17 -3
- data/spec/lib/active_mocker/generate_spec.rb +8 -6
- data/spec/lib/active_mocker/model_reader_spec.rb +5 -0
- data/spec/lib/active_mocker/schema_reader_spec.rb +1 -1
- data/spec/lib/readme_spec.rb +199 -205
- data/spec/unit_logger.rb +24 -0
- metadata +22 -32
- data/mocks/micropost_mock.rb +0 -108
- data/mocks/relationship_mock.rb +0 -109
- data/mocks/user_mock.rb +0 -199
- data/plan_mock.rb +0 -2323
- data/sample_app_rails_4/config/cucumber.yml +0 -8
- data/sample_app_rails_4/db/development.sqlite3 +0 -0
- data/sample_app_rails_4/db/test.sqlite3 +0 -0
- data/spec/lib/active_mocker/performance/base_spec.rb +0 -454
- data/spec/lib/active_mocker/performance/large_schema.rb +0 -3576
- data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
- data/spec/lib/active_mocker/performance/schema_reader_spec.rb +0 -96
- data/spec/lib/compare_mocker_and_record_spec.rb +0 -133
data/lib/active_mocker/logger.rb
CHANGED
@@ -2,11 +2,11 @@ module ActiveMocker
|
|
2
2
|
module MockClassMethods
|
3
3
|
|
4
4
|
def mock_instance_method(method, &block)
|
5
|
-
model_instance_methods[method] = block
|
5
|
+
model_instance_methods[method.to_s] = block
|
6
6
|
end
|
7
7
|
|
8
8
|
def mock_class_method(method, &block)
|
9
|
-
model_class_methods[method] = block
|
9
|
+
model_class_methods[method.to_s] = block
|
10
10
|
end
|
11
11
|
|
12
12
|
def column_names
|
@@ -73,6 +73,16 @@ module MockClassMethods
|
|
73
73
|
return @association_template
|
74
74
|
end
|
75
75
|
|
76
|
+
def set_type(field_name, type)
|
77
|
+
types[field_name] = Virtus::Attribute.build(type)
|
78
|
+
end
|
79
|
+
|
80
|
+
def coerce(field, new_val)
|
81
|
+
type = self.class.types[field]
|
82
|
+
return attributes[field] = type.coerce(new_val) unless type.nil?
|
83
|
+
return new_value
|
84
|
+
end
|
85
|
+
|
76
86
|
public
|
77
87
|
def is_implemented(val, method)
|
78
88
|
raise "#{method} is not Implemented for Class: #{name}" if val == :not_implemented
|
@@ -1,6 +1,7 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
module MockInstanceMethods
|
3
3
|
|
4
|
+
|
4
5
|
def mock_instance_method(method, &block)
|
5
6
|
model_instance_methods[method] = block
|
6
7
|
end
|
@@ -38,11 +39,11 @@ module MockInstanceMethods
|
|
38
39
|
private
|
39
40
|
|
40
41
|
def read_attribute(attr)
|
41
|
-
attributes[attr]
|
42
|
+
@attributes[attr]
|
42
43
|
end
|
43
44
|
|
44
45
|
def write_attribute(attr, value)
|
45
|
-
attributes[attr] = value
|
46
|
+
@attributes[attr] = value
|
46
47
|
end
|
47
48
|
|
48
49
|
def read_association(attr)
|
@@ -6,6 +6,13 @@ class <%= class_name %> < ::ActiveHash::Base
|
|
6
6
|
include ActiveMocker::MockInstanceMethods
|
7
7
|
extend ActiveMocker::MockClassMethods
|
8
8
|
|
9
|
+
def initialize(attributes = {})
|
10
|
+
@attributes = HashWithIndifferentAccess.new(<%= default_attributes %>)
|
11
|
+
@associations = HashWithIndifferentAccess.new(<%= associations %>)
|
12
|
+
super(attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
9
16
|
def self.column_names
|
10
17
|
<%= attribute_names %>
|
11
18
|
end
|
@@ -18,12 +25,13 @@ class <%= class_name %> < ::ActiveHash::Base
|
|
18
25
|
# Attributes getter/setters #
|
19
26
|
##################################
|
20
27
|
<% attributes.each do |meth| %>
|
21
|
-
def <%= meth %>
|
22
|
-
|
23
|
-
|
28
|
+
def <%= meth.name %>
|
29
|
+
@attributes['<%= meth.name %>']
|
30
|
+
end
|
24
31
|
|
25
|
-
def <%= meth %>=(val)
|
26
|
-
|
32
|
+
def <%= meth.name %>=(val)
|
33
|
+
type = (types[:<%= meth.name %>] ||= Virtus::Attribute.build(<%= meth.type %>))
|
34
|
+
@attributes['<%= meth.name %>'] = type.coerce(val)
|
27
35
|
end
|
28
36
|
<% end %>
|
29
37
|
##################################
|
@@ -41,32 +49,27 @@ class <%= class_name %> < ::ActiveHash::Base
|
|
41
49
|
def <%= meth %>=(val)
|
42
50
|
associations['<%= meth %>'] = val
|
43
51
|
end
|
44
|
-
<% end
|
52
|
+
<% end -%>
|
45
53
|
<% collection_associations.each do |meth| %>
|
46
54
|
def <%= meth %>
|
47
|
-
associations['<%= meth %>']
|
55
|
+
associations['<%= meth %>'] ||= ActiveMocker::CollectionAssociation.new
|
48
56
|
end
|
49
57
|
|
50
58
|
def <%= meth %>=(val)
|
51
59
|
associations['<%= meth %>'] = ActiveMocker::CollectionAssociation.new(val)
|
52
60
|
end
|
53
|
-
<% end
|
61
|
+
<% end -%>
|
62
|
+
|
54
63
|
##################################
|
55
64
|
# Model Methods getter/setters #
|
56
65
|
##################################
|
57
66
|
|
58
67
|
def self.model_instance_methods
|
59
|
-
|
60
|
-
@model_instance_methods = {}<% model_instance_methods.each do |key, value| %>
|
61
|
-
@model_instance_methods[:<%= key %>] = :not_implemented<% end %>
|
62
|
-
@model_instance_methods
|
68
|
+
@model_instance_methods ||= <%= model_instance_methods %>
|
63
69
|
end
|
64
70
|
|
65
71
|
def self.model_class_methods
|
66
|
-
|
67
|
-
@model_class_methods = {}<% model_class_methods.each do |key, value| %>
|
68
|
-
@model_class_methods[:<%= key %>] = :not_implemented<% end %>
|
69
|
-
@model_class_methods
|
72
|
+
@model_class_methods ||= <%= model_class_methods %>
|
70
73
|
end
|
71
74
|
|
72
75
|
def self.clear_mock
|
@@ -75,17 +78,17 @@ class <%= class_name %> < ::ActiveHash::Base
|
|
75
78
|
end
|
76
79
|
<% instance_methods.each do |method| %>
|
77
80
|
def <%= method.method %>(<%= method.params %>)
|
78
|
-
block = model_instance_methods[<%= method.method
|
79
|
-
self.class.is_implemented(block,
|
81
|
+
block = model_instance_methods['<%= method.method %>']
|
82
|
+
self.class.is_implemented(block, '#<%= method.method %>')
|
80
83
|
instance_exec(*[<%= method.params_pass %>], &block)
|
81
84
|
end
|
82
|
-
<% end
|
85
|
+
<% end -%>
|
83
86
|
<% class_methods.each do |method| %>
|
84
87
|
def self.<%= method.method %>(<%= method.params %>)
|
85
|
-
block = model_class_methods[<%= method.method
|
86
|
-
is_implemented(block,
|
88
|
+
block = model_class_methods['<%= method.method %>']
|
89
|
+
is_implemented(block, '::<%= method.method %>')
|
87
90
|
instance_exec(*[<%= method.params_pass %>], &block)
|
88
91
|
end
|
89
|
-
<% end
|
92
|
+
<% end -%>
|
90
93
|
|
91
94
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
|
3
|
-
def self.mock(model_name,
|
4
|
-
Generate.mock(model_name
|
3
|
+
def self.mock(model_name, options=nil)
|
4
|
+
Generate.mock(model_name)
|
5
5
|
end
|
6
6
|
|
7
7
|
def self.configure(&block)
|
@@ -12,4 +12,8 @@ module ActiveMocker
|
|
12
12
|
Generate.configure(&block)
|
13
13
|
end
|
14
14
|
|
15
|
+
def self.create_mocks
|
16
|
+
Generate.new
|
17
|
+
end
|
18
|
+
|
15
19
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
class Micropost < ActiveRecord::Base
|
2
2
|
belongs_to :user
|
3
3
|
default_scope -> { order('created_at DESC') }
|
4
|
-
validates :content, presence: true, length: { maximum: 140 }
|
5
|
-
validates :user_id, presence: true
|
6
4
|
|
7
5
|
# Returns microposts from the users being followed by the given user.
|
8
6
|
def self.from_users_followed_by(user)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# This file was generated by Bundler.
|
4
|
+
#
|
5
|
+
# The application 'rspec' is installed as part of a gem, and
|
6
|
+
# this file is here to facilitate running it.
|
7
|
+
#
|
8
|
+
|
9
|
+
require 'pathname'
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
+
Pathname.new(__FILE__).realpath)
|
12
|
+
|
13
|
+
require 'rubygems'
|
14
|
+
require 'bundler/setup'
|
15
|
+
|
16
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
@@ -2,9 +2,6 @@
|
|
2
2
|
|
3
3
|
# Pick the frameworks you want:
|
4
4
|
require "active_record/railtie"
|
5
|
-
require "action_controller/railtie"
|
6
|
-
require "action_mailer/railtie"
|
7
|
-
require "sprockets/railtie"
|
8
5
|
# require "rails/test_unit/railtie"
|
9
6
|
|
10
7
|
# Assets should be precompiled for production (so we don't need the gems loaded then)
|
@@ -12,17 +12,16 @@ development:
|
|
12
12
|
# Warning: The database defined as "test" will be erased and
|
13
13
|
# re-generated from your development database when you run "rake".
|
14
14
|
# Do not set this db to the same as development or production.
|
15
|
-
test:
|
15
|
+
test:
|
16
16
|
adapter: sqlite3
|
17
|
-
|
17
|
+
encoding: utf8
|
18
18
|
pool: 5
|
19
19
|
timeout: 5000
|
20
|
+
database: ":memory:"
|
21
|
+
verbosity: quiet
|
20
22
|
|
21
23
|
production:
|
22
24
|
adapter: sqlite3
|
23
25
|
database: db/production.sqlite3
|
24
26
|
pool: 5
|
25
|
-
timeout: 5000
|
26
|
-
|
27
|
-
cucumber:
|
28
|
-
<<: *test
|
27
|
+
timeout: 5000
|
@@ -11,10 +11,8 @@ SampleApp::Application.configure do
|
|
11
11
|
|
12
12
|
# Show full error reports and disable caching.
|
13
13
|
config.consider_all_requests_local = true
|
14
|
-
config.action_controller.perform_caching = false
|
15
14
|
|
16
15
|
# Don't care if the mailer can't send.
|
17
|
-
config.action_mailer.raise_delivery_errors = false
|
18
16
|
|
19
17
|
# Print deprecation notices to the Rails logger.
|
20
18
|
config.active_support.deprecation = :log
|
@@ -29,7 +29,6 @@ SampleApp::Application.configure do
|
|
29
29
|
# Tell Action Mailer not to deliver emails to the real world.
|
30
30
|
# The :test delivery method accumulates sent emails in the
|
31
31
|
# ActionMailer::Base.deliveries array.
|
32
|
-
config.action_mailer.delivery_method = :test
|
33
32
|
|
34
33
|
# Print deprecation notices to the stderr.
|
35
34
|
config.active_support.deprecation = :stderr
|
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'active_mocker'
|
2
|
-
|
2
|
+
root = APP_ROOT unless defined? Rails
|
3
|
+
root = Rails.root if defined? Rails
|
4
|
+
require "#{root}/lib/unit_logger"
|
3
5
|
ActiveMocker::Generate.configure do |config|
|
4
6
|
# Required Options
|
5
|
-
config.schema_file = File.join(
|
6
|
-
config.model_dir = File.join(
|
7
|
-
config.mock_dir = File.join(
|
7
|
+
config.schema_file = File.join(root, 'db/schema.rb')
|
8
|
+
config.model_dir = File.join(root, 'app/models')
|
9
|
+
config.mock_dir = File.join(root, 'spec/mocks')
|
8
10
|
# Logging
|
9
|
-
config.
|
11
|
+
config.logger = UnitLogger
|
10
12
|
end
|
@@ -35,12 +35,13 @@ ActiveRecord::Schema.define(version: 20130315230445) do
|
|
35
35
|
|
36
36
|
create_table "users", force: true do |t|
|
37
37
|
t.string "name"
|
38
|
-
t.string "email"
|
38
|
+
t.string "email", default: ""
|
39
|
+
t.decimal "credits"
|
39
40
|
t.datetime "created_at"
|
40
41
|
t.datetime "updated_at"
|
41
42
|
t.string "password_digest"
|
42
|
-
t.
|
43
|
-
t.boolean "admin"
|
43
|
+
t.boolean "remember_token", default: true
|
44
|
+
t.boolean "admin", default: false
|
44
45
|
end
|
45
46
|
|
46
47
|
add_index "users", ["email"], name: "index_users_on_email", unique: true
|
@@ -1,10 +1,10 @@
|
|
1
|
+
task rebuild_mocks: :environment do
|
2
|
+
puts 'rebuilding mocks'
|
3
|
+
ActiveMocker.create_mocks
|
4
|
+
end
|
5
|
+
|
1
6
|
['db:schema:load', 'db:migrate', 'db:reset'].each do |task|
|
2
7
|
Rake::Task[task].enhance do
|
3
8
|
Rake::Task['rebuild_mocks'].invoke
|
4
9
|
end
|
5
10
|
end
|
6
|
-
|
7
|
-
task rebuild_mocks: :environment do
|
8
|
-
puts 'rebuilding mocks'
|
9
|
-
ActiveMocker::Generate.new
|
10
|
-
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'logger'
|
2
|
+
class UnitLogger
|
3
|
+
|
4
|
+
def self.method_missing(meth, *args, &block)
|
5
|
+
return Rails.logger.send(meth, *args, &block) if defined? Rails
|
6
|
+
return unit.send(meth, *args, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.unit
|
10
|
+
return @logger unless @logger.nil?
|
11
|
+
@logger = Logger.new(STDOUT)
|
12
|
+
@logger.formatter = proc do |severity, datetime, progname, msg|
|
13
|
+
msg
|
14
|
+
end
|
15
|
+
@logger.level = Logger::DEBUG
|
16
|
+
@logger
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
@@ -12,6 +12,7 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
12
12
|
Micropost.destroy_all
|
13
13
|
end
|
14
14
|
|
15
|
+
|
15
16
|
let(:attributes) { {name: 'Dustin Zeisler', email: 'dustin@example.com'} }
|
16
17
|
let(:attributes_with_admin) { {name: 'Dustin Zeisler', email: 'dustin@example.com', admin: true} }
|
17
18
|
|
@@ -43,8 +44,12 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
43
44
|
let(:user_ar){User.new(attributes)}
|
44
45
|
let(:user_mock){UserMock.new(attributes)}
|
45
46
|
|
46
|
-
it '
|
47
|
-
expect(user_mock.attributes).to eq
|
47
|
+
it 'mock' do
|
48
|
+
expect(user_mock.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "credits" => nil, "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => true, "admin" => false})
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'AR' do
|
52
|
+
expect(user_ar.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "credits" => nil, "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => true, "admin" => false})
|
48
53
|
end
|
49
54
|
|
50
55
|
end
|
@@ -58,12 +63,12 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
58
63
|
let(:user_mock){UserMock.new(create_attributes)}
|
59
64
|
|
60
65
|
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" =>
|
66
|
+
expect(user_mock.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "credits" => nil, "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => true, "admin" => false})
|
62
67
|
expect(user_mock.microposts).to eq [micropost]
|
63
68
|
end
|
64
69
|
|
65
70
|
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" =>
|
71
|
+
expect(user_ar.attributes).to eq({"id" => nil, "name" => "Dustin Zeisler", "email" => "dustin@example.com", "credits" => nil, "created_at" => nil, "updated_at" => nil, "password_digest" => nil, "remember_token" => true, "admin" => false})
|
67
72
|
expect(user_ar.microposts).to eq [micropost]
|
68
73
|
end
|
69
74
|
|
@@ -113,4 +118,105 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
|
|
113
118
|
|
114
119
|
end
|
115
120
|
|
121
|
+
describe 'type coercion' do
|
122
|
+
|
123
|
+
it 'will coerce string to integer' do
|
124
|
+
expect(Micropost.new(user_id: '1').user_id).to eq 1
|
125
|
+
expect(MicropostMock.new(user_id: '1').user_id).to eq 1
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'will coerce string to bool' do
|
129
|
+
expect(User.new(admin: 'true').admin).to eq true
|
130
|
+
expect(UserMock.new(admin: 'true').admin).to eq true
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'will coerce string to decimal' do
|
134
|
+
expect(User.new(credits: '12345').credits).to eq 12345.0
|
135
|
+
expect(UserMock.new(credits: '12345').credits).to eq 12345.0
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'will coerce string to datetime' do
|
139
|
+
expect(User.new(created_at: '1/1/1990').created_at).to eq 'Mon, 01 Jan 1990 00:00:00 UTC +00:00'
|
140
|
+
expect(UserMock.new(created_at: '1/1/1990').created_at).to eq 'Mon, 01 Jan 1990 00:00:00 UTC +00:00'
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'will coerce integer to string' do
|
144
|
+
expect(User.create(name: 1).reload.name).to eq '1'
|
145
|
+
expect(UserMock.new(name: 1).name).to eq '1'
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
describe 'CollectionAssociation' do
|
151
|
+
|
152
|
+
let(:support_array_methods) { [:<<, :take, :push, :clear, :first, :last, :concat, :replace, :distinct, :uniq, :count, :size, :length, :empty?, :any?, :include?] }
|
153
|
+
|
154
|
+
it 'supported array methods' do
|
155
|
+
mp1 = Micropost.create!(content: 'text')
|
156
|
+
mp2 = Micropost.create!(content: 'text')
|
157
|
+
user = User.create(microposts: [mp1, mp2])
|
158
|
+
|
159
|
+
mpm1 = MicropostMock.create
|
160
|
+
mpm2 = MicropostMock.create
|
161
|
+
user_mock = UserMock.create(microposts: [mpm1, mpm2])
|
162
|
+
|
163
|
+
expect(user.microposts.methods).to include *support_array_methods
|
164
|
+
expect(user_mock.microposts.methods).to include *support_array_methods
|
165
|
+
expect(user.microposts.take(1).count).to eq(1)
|
166
|
+
expect(user_mock.microposts.take(1).count).to eq(1)
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
it '#sum' do
|
171
|
+
mp1 = Micropost.create!(content: 'text')
|
172
|
+
mp2 = Micropost.create!(content: 'text')
|
173
|
+
user = User.create(microposts: [mp1, mp2])
|
174
|
+
expect(user.microposts.sum(:user_id)).to eq 2
|
175
|
+
|
176
|
+
mpm1 = MicropostMock.create(user_id: 1)
|
177
|
+
mpm2 = MicropostMock.create(user_id: 2)
|
178
|
+
user_mock = UserMock.create(microposts: [mpm1, mpm2])
|
179
|
+
|
180
|
+
expect(user_mock.microposts.sum(:user_id)).to eq 3
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
describe 'default values' do
|
187
|
+
|
188
|
+
context 'if nil values are defaulted' do
|
189
|
+
|
190
|
+
it 'ar' do
|
191
|
+
user = User.new
|
192
|
+
expect(user.admin).to eq false
|
193
|
+
expect(user.remember_token).to eq true
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'mock' do
|
197
|
+
user = UserMock.new
|
198
|
+
expect(user.admin).to eq false
|
199
|
+
expect(user.remember_token).to eq true
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'values can be passed' do
|
205
|
+
|
206
|
+
it 'ar' do
|
207
|
+
user = User.new(admin: true, remember_token: false)
|
208
|
+
expect(user.admin).to eq true
|
209
|
+
expect(user.remember_token).to eq false
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'mock' do
|
213
|
+
user = UserMock.new(admin: true, remember_token: false)
|
214
|
+
expect(user.admin).to eq true
|
215
|
+
expect(user.remember_token).to eq false
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
116
222
|
end
|