Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify mockery and add check to include state in failure message #449

Closed
wants to merge 13 commits into from
Closed
44 changes: 12 additions & 32 deletions lib/mocha/mockery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ def stubba
private

def raise_not_initialized_error
message = 'Mocha methods cannot be used outside the context of a test'
raise NotInitializedError.new(message, caller)
raise NotInitializedError.new('Mocha methods cannot be used outside the context of a test', caller)
end
end

class << self
def instance
instances.last || Null.new
@instances.last || Null.new
end

def setup
@instances ||= []
mockery = new
mockery.logger = instance.logger unless instances.empty?
mockery.logger = instance.logger unless @instances.empty?
@instances.push(mockery)
end

Expand All @@ -52,13 +52,6 @@ def teardown
instance.teardown
ensure
@instances.pop
@instances = nil if instances.empty?
end

private

def instances
@instances ||= []
end
end

Expand All @@ -84,19 +77,11 @@ def new_state_machine(name)

def verify(assertion_counter = nil)
unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
message = "not all expectations were satisfied\n#{mocha_inspect}"
backtrace = if unsatisfied_expectations.empty?
caller
else
unsatisfied_expectations[0].backtrace
end
raise ExpectationErrorFactory.build(message, backtrace)
backtrace = unsatisfied_expectations.empty? ? caller : unsatisfied_expectations[0].backtrace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again I'm not convinced about the long lines this generates.

raise ExpectationErrorFactory.build("not all expectations were satisfied\n#{mocha_inspect}", backtrace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced about the long lines this generates.

end
expectations.each do |e|
unless Mocha.configuration.stubbing_method_unnecessarily == :allow
next if e.used?
on_stubbing_method_unnecessarily(e)
end
expectations.reject(&:used?).each do |e|
check(:stubbing_method_unnecessarily, 'method unnecessarily', e.method_signature, e.backtrace)
end
end

Expand All @@ -119,11 +104,10 @@ def state_machines
end

def mocha_inspect
message = ''
message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless unsatisfied_expectations.empty?
message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" unless satisfied_expectations.empty?
message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}" unless state_machines.empty?
message
[
['unsatisfied expectations', unsatisfied_expectations], ['satisfied expectations', satisfied_expectations],
['states', state_machines]
].map { |label, list| "#{label}:\n- #{list.map(&:mocha_inspect).join("\n- ")}\n" if list.any? }.join
end

def on_stubbing(object, method)
Expand All @@ -139,10 +123,6 @@ def on_stubbing(object, method)
check(:stubbing_method_on_non_mock_object, 'method on non-mock object', method_signature)
end

def on_stubbing_method_unnecessarily(expectation)
check(:stubbing_method_unnecessarily, 'method unnecessarily', expectation.method_signature, expectation.backtrace)
end

attr_writer :logger

def logger
Expand Down
16 changes: 16 additions & 0 deletions test/acceptance/multiple_expectations_failure_message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@ def test_should_report_multiple_satisfied_expectations
'- expected exactly once, invoked once: #<Mock:mock>.method_one(any_parameters)'
], test_result.failure_message_lines
end

def test_should_include_state_in_unsatisfied_expectation_message
test_result = run_as_test do
mock = mock('mock')
readiness = states('readiness')
mock.expects(:method_one).once.then(readiness.is('ready'))
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method_one(any_parameters)',
'states:',
'- readiness has no current state'
], test_result.failure_message_lines
end
end