Skip to content

Commit

Permalink
## Fix
Browse files Browse the repository at this point in the history
* Constant values assigned to non sudo primitives objects
#72
  • Loading branch information
zeisler committed Jun 27, 2016
1 parent dae165d commit 793255a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/active_mocker/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@
require "active_mocker/mock/object_inspect"
require "active_mocker/mock/alias_attribute"
require "active_mocker/mock/base"
require "active_mocker/mock/unrepresentable_const_value"
3 changes: 3 additions & 0 deletions lib/active_mocker/mock/unrepresentable_const_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ActiveMocker
UNREPRESENTABLE_CONST_VALUE = "ActiveMocker can not determine the value, if needed stub this const with a valid test value".freeze
end
20 changes: 18 additions & 2 deletions lib/active_mocker/mock_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,26 @@ def primary_key
end

module ModulesConstants
require "active_mocker/mock/unrepresentable_const_value"

class Inspectable

attr_reader :inspect

def initialize(inspect)
@inspect = inspect
end
end

def constants
class_introspector.get_class.constants.each_with_object({}) do |v, const|
c = class_introspector.get_class.const_get(v)
const[v] = c unless c.class == Module || c.class == Class
c = class_introspector.get_class.const_get(v)
next if [Module, Class].include?(c.class)
if /\A#</ =~ c.inspect
const[v] = Inspectable.new("ActiveMocker::UNREPRESENTABLE_CONST_VALUE")
else
const[v] = c
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/active_mocker/mock_template/_modules_constants.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
<% end -%>
<% modules[:extended].each do |constant| -%>
extend <%= constant %>
<% end -%>
<% end -%>
1 change: 1 addition & 0 deletions spec/lib/active_mocker/mock_creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def table_name
class ModelMock < ActiveMocker::Base
created_with('#{ActiveMocker::VERSION}')
MY_CONSTANT_VALUE = 3
MY_OBJECT = ActiveMocker::UNREPRESENTABLE_CONST_VALUE
prepend ModelStandInForARVersion::PostMethods
end
RUBY
Expand Down
1 change: 1 addition & 0 deletions spec/lib/models/model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
class Model < ActiveRecord::Base
MY_CONSTANT_VALUE = 3
MY_OBJECT = Object.new
module FooBar
end

Expand Down
2 changes: 1 addition & 1 deletion tasks/integration.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ task :integration do
Dir.chdir("test_rails_4_app") do
root = File.expand_path("../../", __FILE__)
gemfiles, error_verbosity = if ENV["TRAVIS"]
[[ENV["BUNDLE_GEMFILE"]], "1"]
[[ENV["BUNDLE_GEMFILE"]], "3"]
else
[Dir[Pathname(File.join(root, "test_rails_4_app/gemfiles/*.gemfile")).expand_path], 0]
end
Expand Down
5 changes: 3 additions & 2 deletions test_rails_4_app/app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# frozen_string_literal: true
require "#{Rails.root}/lib/post_methods"
require "#{Rails.root}/app/models/mircopost/core"

class Micropost < ActiveRecord::Base
require "#{Rails.root}/app/models/mircopost/core"
belongs_to :user
default_scope -> { order("created_at DESC") }
MAGIC_ID_NUMBER = 90
MAGIC_ID_STRING = "F-1"
MAGIC_OBJECT = Object.new

include PostMethods
extend PostMethods
include Core
include Micropost::Core

module DoNotIncludeInMock
def sample_method
Expand Down
2 changes: 1 addition & 1 deletion test_rails_4_app/app/models/mircopost/core.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Micropost
class Micropost < ActiveRecord::Base
module Core
end
end
6 changes: 6 additions & 0 deletions test_rails_4_app/spec/micropost_mock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
end
end

describe "::MAGIC_OBJECT" do
it "has constant from model" do
expect(MicropostMock::MAGIC_OBJECT).to eq ActiveMocker::UNREPRESENTABLE_CONST_VALUE
end
end

context "included methods" do
it "has methods" do
expect(MicropostMock.new.respond_to?(:sample_method)).to eq true
Expand Down

0 comments on commit 793255a

Please sign in to comment.