Skip to content

Commit

Permalink
Hide the internals stack trace when calling method that will raising …
Browse files Browse the repository at this point in the history
…`NotImplementedError`.
  • Loading branch information
zeisler committed Dec 7, 2014
1 parent 76ac8b6 commit df4dea9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 31 deletions.
12 changes: 6 additions & 6 deletions lib/active_mocker/mock/mock_abilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def class_name
self.class
end

def is_implemented(val, method, type)
raise NotImplementedError, "#{type}#{method} for Class: #{class_name}. To continue stub the method." if val.nil?
def is_implemented(val, method, type, call_stack)
raise NotImplementedError, "#{type}#{method} for Class: #{class_name}. To continue stub the method.", call_stack if val.nil?
end

def execute_block(method)
Expand Down Expand Up @@ -55,9 +55,9 @@ def mock_class_method(method, exe_bind=false, &block)

alias_method :stub_class_method, :mock_class_method

def call_mock_method(method, *arguments)
def call_mock_method(method, caller, *arguments)
mock_method = mockable_class_methods[method.to_sym]
is_implemented(mock_method, method, '::')
is_implemented(mock_method, method, '::', caller)
mock_method.arguments = arguments
execute_block(mock_method)
end
Expand All @@ -68,10 +68,10 @@ def call_mock_method(method, *arguments)

include InstanceAndClassMethods

def call_mock_method(method, *arguments)
def call_mock_method(method, caller, *arguments)
mock_method = mockable_instance_methods[method.to_sym]
mock_method = self.class.send(:mockable_instance_methods)[method.to_sym] if mock_method.nil?
is_implemented(mock_method, method, '#')
is_implemented(mock_method, method, '#', caller)
mock_method.arguments = arguments
execute_block mock_method
end
Expand Down
4 changes: 2 additions & 2 deletions lib/active_mocker/mock_template.erb
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ class <%= class_name + @mock_append_name %> < <%= parent_class %>

<% instance_methods.each do |method| %>
def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
call_mock_method :<%= method.name %>, Kernel.caller<%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
end
<% end -%>
<% class_methods.each do |method| %>
def self.<%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
call_mock_method :<%= method.name %>, Kernel.caller<%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
end
<% end -%>

Expand Down
35 changes: 22 additions & 13 deletions spec/lib/active_mocker/mock/mockablies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestClass
include ActiveMocker::Mock::MockAbilities

def self.buz
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [])
end

def pop
Expand Down Expand Up @@ -41,11 +41,11 @@ class TestableBinding
include ActiveMocker::Mock::MockAbilities

def buz
call_mock_method(__method__)
call_mock_method(__method__, caller)
end

def self.baz
call_mock_method(__method__)
call_mock_method(__method__, caller)
end

def self.pop
Expand Down Expand Up @@ -125,7 +125,7 @@ class TestHierarchy
include ActiveMocker::Mock::MockAbilities

def buz
call_mock_method(__method__)
call_mock_method(__method__, caller)
end

def pop
Expand Down Expand Up @@ -159,11 +159,11 @@ class TestBoth
include ActiveMocker::Mock::MockAbilities

def zip
call_mock_method(__method__)
call_mock_method(__method__, caller)
end

def self.wiz
call_mock_method(__method__)
call_mock_method(__method__, caller)
end

end
Expand All @@ -187,7 +187,7 @@ class WithArgs
include ActiveMocker::Mock::MockAbilities

def foo(stuff, other = nil)
call_mock_method(__method__, stuff, other)
call_mock_method(__method__, caller, stuff, other)
end

def pop
Expand All @@ -196,7 +196,7 @@ def pop


def self.fiz(buz)
call_mock_method(__method__, buz)
call_mock_method(__method__, caller, buz)
end

end
Expand Down Expand Up @@ -243,7 +243,7 @@ class TestClearInstance
include ActiveMocker::Mock::MockAbilities

def buz
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [])
end

end
Expand All @@ -266,11 +266,11 @@ class TestClearInstance
include ActiveMocker::Mock::MockAbilities

def buz
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [])
end

def self.pop
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [])
end

end
Expand All @@ -294,20 +294,29 @@ class TestRaise
include ActiveMocker::Mock::MockAbilities

def buz
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [])
end

def self.pop
call_mock_method(__method__, [])
call_mock_method(__method__, caller, [],)
end

end
end

it 'will raise if unmocked class method is called' do

expect{ TestRaise.pop}.to raise_error(ActiveMocker::Mock::NotImplementedError, '::pop for Class: TestRaise. To continue stub the method.')
end

it 'will start the backtrace at the point where the method was called' do
begin
TestRaise.pop
rescue ActiveMocker::Mock::NotImplementedError => e
expect(e.backtrace.first).to match(/active_mocker\/mock\/mockablies_spec.rb/)
end
end

it 'will raise if unmocked instance method is called' do
expect{ TestRaise.new.buz}.to raise_error(ActiveMocker::Mock::NotImplementedError, '#buz for Class: TestRaise. To continue stub the method.')
end
Expand Down
2 changes: 1 addition & 1 deletion test_rails_4_app/spec/mocks/child_model_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def self.new_relation(collection)


def child_method
call_mock_method :child_method
call_mock_method :child_method, Kernel.caller
end

end
6 changes: 3 additions & 3 deletions test_rails_4_app/spec/mocks/micropost_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ def self.new_relation(collection)


def display_name
call_mock_method :display_name
call_mock_method :display_name, Kernel.caller
end

def post_id
call_mock_method :post_id
call_mock_method :post_id, Kernel.caller
end

def self.from_users_followed_by(user=nil)
call_mock_method :from_users_followed_by, user
call_mock_method :from_users_followed_by, Kernel.caller, user
end

end
12 changes: 6 additions & 6 deletions test_rails_4_app/spec/mocks/user_mock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,27 @@ def self.new_relation(collection)


def feed
call_mock_method :feed
call_mock_method :feed, Kernel.caller
end

def following?(other_user)
call_mock_method :following?, other_user
call_mock_method :following?, Kernel.caller, other_user
end

def follow!(other_user)
call_mock_method :follow!, other_user
call_mock_method :follow!, Kernel.caller, other_user
end

def unfollow!(other_user)
call_mock_method :unfollow!, other_user
call_mock_method :unfollow!, Kernel.caller, other_user
end

def self.new_remember_token
call_mock_method :new_remember_token
call_mock_method :new_remember_token, Kernel.caller
end

def self.digest(token)
call_mock_method :digest, token
call_mock_method :digest, Kernel.caller, token
end

end

0 comments on commit df4dea9

Please sign in to comment.