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 51321fa
Show file tree
Hide file tree
Showing 5 changed files with 20 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 @@ -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
11 changes: 9 additions & 2 deletions lib/active_mocker/mock_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,17 @@ def primary_key
end

module ModulesConstants
require "active_mocker/mock/unrepresentable_const_value"

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] = ActiveMocker::UNREPRESENTABLE_CONST_VALUE
else
const[v] = c
end
end
end

Expand Down
1 change: 1 addition & 0 deletions test_rails_4_app/app/models/micropost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Micropost < ActiveRecord::Base
default_scope -> { order("created_at DESC") }
MAGIC_ID_NUMBER = 90
MAGIC_ID_STRING = "F-1"
MAGIC_OBJECT = Object.new

include PostMethods
extend PostMethods
Expand Down
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 51321fa

Please sign in to comment.