Skip to content

Commit

Permalink
Fix: Constant values assigned to non sudo primitives objects
Browse files Browse the repository at this point in the history
  • Loading branch information
zeisler committed Jun 27, 2016
1 parent dae165d commit 6033a8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/active_mocker/mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
require "active_mocker/mock/records"
require "active_mocker/mock/object_inspect"
require "active_mocker/mock/alias_attribute"
require "active_mocker/mock/unrepresentable_const_value"
require "active_mocker/mock/base"
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
19 changes: 17 additions & 2 deletions lib/active_mocker/mock_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,25 @@ 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
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

0 comments on commit 6033a8d

Please sign in to comment.