Skip to content

Commit

Permalink
Simplify Mockery
Browse files Browse the repository at this point in the history
Closes #449.
  • Loading branch information
floehopper committed Feb 10, 2020
2 parents fc5ea2f + c7db3f6 commit cc91b87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
31 changes: 9 additions & 22 deletions lib/mocha/mockery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ def raise_not_initialized_error

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 +53,6 @@ def teardown
instance.teardown
ensure
@instances.pop
@instances = nil if instances.empty?
end

private

def instances
@instances ||= []
end
end

Expand Down Expand Up @@ -92,11 +86,9 @@ def verify(assertion_counter = nil)
end
raise ExpectationErrorFactory.build(message, backtrace)
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 |expectation|
signature_proc = lambda { expectation.method_signature }
check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, expectation.backtrace)
end
end

Expand All @@ -120,9 +112,9 @@ def state_machines

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:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any?
message << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any?
message << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any?
message
end

Expand All @@ -139,11 +131,6 @@ def on_stubbing(object, method)
check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc)
end

def on_stubbing_method_unnecessarily(expectation)
signature_proc = lambda { expectation.method_signature }
check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, 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

0 comments on commit cc91b87

Please sign in to comment.