Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #10 from tristandunn/trailing-whitespace

Remove trailing whitespace.
  • Loading branch information...
commit 5a39f667def07a379c8d4362b9d533599318ebb4 2 parents 4bbe610 + c3cc6bd
@gabebw gabebw authored
View
18 test/acceptance/acceptance_test_helper.rb
@@ -3,25 +3,25 @@
require 'mocha/configuration'
module AcceptanceTest
-
+
class FakeLogger
-
+
attr_reader :warnings
-
+
def initialize
@warnings = []
end
-
+
def warn(message)
@warnings << message
end
-
+
end
-
+
attr_reader :logger
include TestRunner
-
+
def setup_acceptance_test
Mocha::Configuration.reset_configuration
@logger = FakeLogger.new
@@ -29,10 +29,10 @@ def setup_acceptance_test
@original_logger = mockery.logger
mockery.logger = @logger
end
-
+
def teardown_acceptance_test
Mocha::Configuration.reset_configuration
Mocha::Mockery.instance.logger = @original_logger
end
-
+
end
View
52 test/acceptance/mocha_example_test.rb
@@ -2,97 +2,97 @@
require 'mocha'
class MochaExampleTest < Test::Unit::TestCase
-
+
class Rover
-
+
def initialize(left_track, right_track, steps_per_metre, steps_per_degree)
@left_track, @right_track, @steps_per_metre, @steps_per_degree = left_track, right_track, steps_per_metre, steps_per_degree
end
-
+
def forward(metres)
@left_track.step(metres * @steps_per_metre)
@right_track.step(metres * @steps_per_metre)
wait
end
-
+
def backward(metres)
forward(-metres)
end
-
+
def left(degrees)
@left_track.step(-degrees * @steps_per_degree)
@right_track.step(+degrees * @steps_per_degree)
wait
end
-
+
def right(degrees)
left(-degrees)
end
-
+
def wait
while (@left_track.moving? or @right_track.moving?); end
end
-
+
end
-
+
def test_should_step_both_tracks_forward_ten_steps
left_track = mock('left_track')
right_track = mock('right_track')
steps_per_metre = 5
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
-
+
left_track.expects(:step).with(10)
right_track.expects(:step).with(10)
-
+
left_track.stubs(:moving?).returns(false)
right_track.stubs(:moving?).returns(false)
-
+
rover.forward(2)
end
-
+
def test_should_step_both_tracks_backward_ten_steps
left_track = mock('left_track')
right_track = mock('right_track')
steps_per_metre = 5
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
-
+
left_track.expects(:step).with(-10)
right_track.expects(:step).with(-10)
-
+
left_track.stubs(:moving?).returns(false)
right_track.stubs(:moving?).returns(false)
-
+
rover.backward(2)
end
-
+
def test_should_step_left_track_forwards_five_steps_and_right_track_backwards_five_steps
left_track = mock('left_track')
right_track = mock('right_track')
steps_per_degree = 5.0 / 90.0
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
-
+
left_track.expects(:step).with(+5)
right_track.expects(:step).with(-5)
-
+
left_track.stubs(:moving?).returns(false)
right_track.stubs(:moving?).returns(false)
-
+
rover.right(90)
end
-
+
def test_should_step_left_track_backwards_five_steps_and_right_track_forwards_five_steps
left_track = mock('left_track')
right_track = mock('right_track')
steps_per_degree = 5.0 / 90.0
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
-
+
left_track.expects(:step).with(-5)
right_track.expects(:step).with(+5)
-
+
left_track.stubs(:moving?).returns(false)
right_track.stubs(:moving?).returns(false)
-
+
rover.left(90)
end
-
-end
+
+end
View
8 test/acceptance/spy_test.rb
@@ -7,11 +7,11 @@ module SpyTestMethods
def setup
setup_acceptance_test
end
-
+
def teardown
teardown_acceptance_test
end
-
+
def test_should_accept_wildcard_stub_call_without_arguments
instance = new_instance
instance.stubs(:magic)
@@ -154,10 +154,10 @@ class StubEverythingSpyTest < Test::Unit::TestCase
def setup
setup_acceptance_test
end
-
+
def teardown
teardown_acceptance_test
- end
+ end
def test_should_match_invocations_with_no_explicit_stubbing
instance = stub_everything
instance.surprise!
View
48 test/acceptance/stubba_example_test.rb
@@ -2,86 +2,86 @@
require 'mocha'
class Widget
-
+
def model
'original_model'
end
-
+
class << self
-
+
def find(options)
[]
end
-
+
def create(attributes)
Widget.new
end
-
+
end
-
+
end
module Thingy
-
+
def self.wotsit
:hoojamaflip
end
-
+
end
class StubbaExampleTest < Test::Unit::TestCase
-
+
def test_should_stub_instance_method
widget = Widget.new
widget.expects(:model).returns('different_model')
assert_equal 'different_model', widget.model
end
-
+
def test_should_stub_module_method
should_stub_module_method
end
-
+
def test_should_stub_module_method_again
should_stub_module_method
end
-
+
def test_should_stub_class_method
should_stub_class_method
end
-
+
def test_should_stub_class_method_again
should_stub_class_method
end
-
+
def test_should_stub_instance_method_on_any_instance_of_a_class
should_stub_instance_method_on_any_instance_of_a_class
end
-
+
def test_should_stub_instance_method_on_any_instance_of_a_class_again
should_stub_instance_method_on_any_instance_of_a_class
end
-
+
def test_should_stub_two_different_class_methods
should_stub_two_different_class_methods
end
-
+
def test_should_stub_two_different_class_methods_again
should_stub_two_different_class_methods
end
-
+
private
-
+
def should_stub_module_method
Thingy.expects(:wotsit).returns(:dooda)
assert_equal :dooda, Thingy.wotsit
end
-
+
def should_stub_class_method
widgets = [Widget.new]
Widget.expects(:find).with(:all).returns(widgets)
assert_equal widgets, Widget.find(:all)
- end
-
+ end
+
def should_stub_two_different_class_methods
found_widgets = [Widget.new]
created_widget = Widget.new
@@ -90,7 +90,7 @@ def should_stub_two_different_class_methods
assert_equal found_widgets, Widget.find(:all)
assert_equal created_widget, Widget.create(:model => 'wombat')
end
-
+
def should_stub_instance_method_on_any_instance_of_a_class
Widget.any_instance.expects(:model).at_least_once.returns('another_model')
widget_1 = Widget.new
@@ -99,4 +99,4 @@ def should_stub_instance_method_on_any_instance_of_a_class
assert_equal 'another_model', widget_2.model
end
-end
+end
View
14 test/execution_point.rb
@@ -1,20 +1,20 @@
class ExecutionPoint
-
+
attr_reader :backtrace
def self.current
new(caller)
end
-
+
def initialize(backtrace)
@backtrace = backtrace
end
-
+
def file_name
return "unknown" unless @backtrace && @backtrace.first
/\A(.*?):\d+/.match(@backtrace.first)[1]
end
-
+
def line_number
return "unknown" unless @backtrace && @backtrace.first
Integer(/\A.*?:(\d+)/.match(@backtrace.first)[1])
@@ -24,13 +24,13 @@ def ==(other)
return false unless other.is_a?(ExecutionPoint)
(file_name == other.file_name) and (line_number == other.line_number)
end
-
+
def to_s
"file: #{file_name}; line: #{line_number}"
end
-
+
def inspect
to_s
end
-
+
end
View
4 test/method_definer.rb
@@ -1,7 +1,7 @@
require 'metaclass'
module Mocha
-
+
module ObjectMethods
def define_instance_method(method_symbol, &block)
__metaclass__.send(:define_method, method_symbol, block)
@@ -16,7 +16,7 @@ def define_instance_accessor(*symbols)
symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
end
end
-
+
end
class Object
View
32 test/mini_test_result.rb
@@ -3,30 +3,30 @@
require 'minitest/unit'
class MiniTestResult
-
+
FAILURE_PATTERN = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m
ERROR_PATTERN = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n}m
-
+
def self.parse_failure(raw)
matches = FAILURE_PATTERN.match(raw)
return nil unless matches
Failure.new(matches[2], matches[3], [matches[4]], matches[5])
end
-
+
def self.parse_error(raw)
matches = ERROR_PATTERN.match(raw)
return nil unless matches
backtrace = raw.gsub(ERROR_PATTERN, '').split("\n").map(&:strip)
Error.new(matches[2], matches[3], matches[4], backtrace)
end
-
+
class Failure
attr_reader :method, :test_case, :location, :message
def initialize(method, test_case, location, message)
@method, @test_case, @location, @message = method, test_case, location, message
end
end
-
+
class Error
class Exception
attr_reader :message, :backtrace
@@ -34,49 +34,49 @@ def initialize(message, location)
@message, @backtrace = message, location
end
end
-
+
attr_reader :method, :test_case, :exception
def initialize(method, test_case, message, backtrace)
@method, @test_case, @exception = method, test_case, Exception.new(message, backtrace)
end
end
-
+
def initialize(runner, test)
@runner, @test = runner, test
end
-
+
def failure_count
@runner.failures
end
-
+
def assertion_count
@test._assertions
end
-
+
def error_count
@runner.errors
end
-
+
def passed?
@test.passed?
end
-
+
def failures
@runner.report.map { |puked| MiniTestResult.parse_failure(puked) }.compact
end
-
+
def errors
@runner.report.map { |puked| MiniTestResult.parse_error(puked) }.compact
end
-
+
def failure_messages
failures.map(&:message)
end
-
+
def failure_message_lines
failure_messages.map { |message| message.split("\n") }.flatten
end
-
+
def error_messages
errors.map { |e| e.exception.message }
end
View
10 test/simple_counter.rb
@@ -1,13 +1,13 @@
class SimpleCounter
-
+
attr_reader :count
-
+
def initialize
@count = 0
end
-
+
def increment
@count += 1
end
-
-end
+
+end
View
6 test/test_runner.rb
@@ -13,7 +13,7 @@ def run_as_test(test_result = nil, &block)
define_method(:test_me, &block)
end
test = test_class.new(:test_me)
-
+
if defined?(Test::Unit::TestResult)
test_result ||= Test::Unit::TestResult.new
test.run(test_result) {}
@@ -34,10 +34,10 @@ def error_messages
test.run(runner)
test_result = MiniTestResult.new(runner, test)
end
-
+
test_result
end
-
+
def assert_passed(test_result)
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0
View
130 test/unit/expectation_test.rb
@@ -7,7 +7,7 @@
require 'simple_counter'
class ExpectationTest < Test::Unit::TestCase
-
+
include Mocha
class FakeMockery
@@ -36,67 +36,67 @@ def teardown
def new_expectation
Expectation.new(nil, :expected_method)
end
-
+
def test_should_match_calls_to_same_method_with_any_parameters
assert new_expectation.match?(:expected_method, 1, 2, 3)
end
-
+
def test_should_match_calls_to_same_method_with_exactly_zero_parameters
expectation = new_expectation.with()
assert expectation.match?(:expected_method)
end
-
+
def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
expectation = new_expectation.with()
assert !expectation.match?(:expected_method, 1, 2, 3)
end
-
+
def test_should_match_calls_to_same_method_with_expected_parameter_values
expectation = new_expectation.with(1, 2, 3)
assert expectation.match?(:expected_method, 1, 2, 3)
end
-
+
def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
expectation = new_expectation.with() {|x, y, z| x + y == z}
assert expectation.match?(:expected_method, 1, 2, 3)
end
-
+
def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
expectation = new_expectation.with() {|x, y, z| x + y == z}
assert !expectation.match?(:different_method, 1, 2, 3)
end
-
+
def test_should_not_match_calls_to_different_methods_with_no_parameters
assert !new_expectation.match?(:unexpected_method)
end
-
+
def test_should_not_match_calls_to_same_method_with_too_few_parameters
expectation = new_expectation.with(1, 2, 3)
assert !expectation.match?(:unexpected_method, 1, 2)
end
-
+
def test_should_not_match_calls_to_same_method_with_too_many_parameters
expectation = new_expectation.with(1, 2)
assert !expectation.match?(:unexpected_method, 1, 2, 3)
end
-
+
def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
expectation = new_expectation.with(1, 2, 3)
assert !expectation.match?(:unexpected_method, 1, 0, 3)
end
-
+
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
expectation = new_expectation.with() {|x, y, z| x + y == z}
assert !expectation.match?(:expected_method, 1, 0, 3)
end
-
+
def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
expectation = new_expectation.times(1)
assert expectation.invocations_allowed?
expectation.invoke([])
assert !expectation.invocations_allowed?
end
-
+
def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
expectation = new_expectation.times(2)
assert expectation.invocations_allowed?
@@ -116,18 +116,18 @@ def test_should_allow_invocations_until_expected_invocation_count_is_a_range_fro
expectation.invoke([])
assert !expectation.invocations_allowed?
end
-
+
def test_should_store_provided_backtrace
backtrace = Object.new
expectation = Expectation.new(nil, :expected_method, backtrace)
assert_equal backtrace, expectation.backtrace
end
-
+
def test_should_default_backtrace_to_caller
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
end
-
+
def test_should_not_yield
yielded = false
new_expectation.invoke([]) { yielded = true }
@@ -155,7 +155,7 @@ def test_should_yield_different_parameters_on_consecutive_invocations
expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
end
-
+
def test_should_yield_multiple_times_for_single_invocation
expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
yielded_parameters = []
@@ -175,20 +175,20 @@ def test_should_return_specified_value
expectation = new_expectation.returns(99)
assert_equal 99, expectation.invoke([])
end
-
+
def test_should_return_same_specified_value_multiple_times
expectation = new_expectation.returns(99)
assert_equal 99, expectation.invoke([])
assert_equal 99, expectation.invoke([])
end
-
+
def test_should_return_specified_values_on_consecutive_calls
expectation = new_expectation.returns(99, 100, 101)
assert_equal 99, expectation.invoke([])
assert_equal 100, expectation.invoke([])
assert_equal 101, expectation.invoke([])
end
-
+
def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
values = [99, 100, 101]
expectation = new_expectation.returns(*values)
@@ -197,27 +197,27 @@ def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_
assert_equal 100, expectation.invoke([])
assert_equal 101, expectation.invoke([])
end
-
+
def test_should_return_nil_by_default
assert_nil new_expectation.invoke([])
end
-
+
def test_should_return_nil_if_no_value_specified
expectation = new_expectation.returns()
assert_nil expectation.invoke([])
end
-
+
def test_should_raise_runtime_exception
expectation = new_expectation.raises
assert_raise(RuntimeError) { expectation.invoke([]) }
end
-
+
def test_should_raise_custom_exception
exception = Class.new(Exception)
expectation = new_expectation.raises(exception)
assert_raise(exception) { expectation.invoke([]) }
end
-
+
def test_should_raise_same_instance_of_custom_exception
exception_klass = Class.new(StandardError)
expected_exception = exception_klass.new
@@ -225,40 +225,40 @@ def test_should_raise_same_instance_of_custom_exception
actual_exception = assert_raise(exception_klass) { expectation.invoke([]) }
assert_same expected_exception, actual_exception
end
-
+
def test_should_use_the_default_exception_message
expectation = new_expectation.raises(Exception)
exception = assert_raise(Exception) { expectation.invoke([]) }
assert_equal Exception.new.message, exception.message
end
-
+
def test_should_raise_custom_exception_with_message
exception_msg = "exception message"
expectation = new_expectation.raises(Exception, exception_msg)
exception = assert_raise(Exception) { expectation.invoke([]) }
assert_equal exception_msg, exception.message
end
-
+
def test_should_return_values_then_raise_exception
expectation = new_expectation.returns(1, 2).then.raises()
assert_equal 1, expectation.invoke([])
assert_equal 2, expectation.invoke([])
assert_raise(RuntimeError) { expectation.invoke([]) }
end
-
+
def test_should_raise_exception_then_return_values
expectation = new_expectation.raises().then.returns(1, 2)
assert_raise(RuntimeError) { expectation.invoke([]) }
assert_equal 1, expectation.invoke([])
assert_equal 2, expectation.invoke([])
end
-
+
def test_should_verify_successfully_if_expected_call_was_made
expectation = new_expectation
expectation.invoke([])
assert expectation.verified?
end
-
+
def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
expectation = new_expectation.once
expectation.invoke([])
@@ -303,19 +303,19 @@ def test_should_verify_successfully_if_expected_call_was_made_at_least_once
3.times {expectation.invoke([])}
assert expectation.verified?
end
-
+
def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
expectation = new_expectation.with(1, 2, 3).at_least_once
assert !expectation.verified?
assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
end
-
+
def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
expectation = new_expectation.times(2)
2.times {expectation.invoke([])}
assert expectation.verified?
end
-
+
def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
expectation = new_expectation.times(2)
1.times {expectation.invoke([])}
@@ -323,13 +323,13 @@ def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
assert_match(/expected exactly twice/i, expectation.mocha_inspect)
assert_match(/invoked once/i, expectation.mocha_inspect)
end
-
+
def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
expectation = new_expectation.times(2)
3.times {expectation.invoke([])}
assert !expectation.verified?
end
-
+
def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
expectation = new_expectation
expectation.invoke([])
@@ -337,21 +337,21 @@ def test_should_increment_assertion_counter_for_expectation_because_it_does_need
expectation.verified?(assertion_counter)
assert_equal 1, assertion_counter.count
end
-
+
def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
stub = Expectation.new(nil, :expected_method).at_least(0)
assertion_counter = SimpleCounter.new
stub.verified?(assertion_counter)
assert_equal 0, assertion_counter.count
end
-
+
def test_should_store_backtrace_from_point_where_expectation_was_created
execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
end
-
+
class FakeMock
-
+
def initialize(name)
@name = name
end
@@ -359,9 +359,9 @@ def initialize(name)
def mocha_inspect
@name
end
-
+
end
-
+
def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
mock = FakeMock.new('mock')
sequence_one = Sequence.new('one')
@@ -370,19 +370,19 @@ def test_should_raise_error_with_message_indicating_which_method_was_expected_to
assert !expectation.verified?
assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
end
-
+
class FakeConstraint
-
+
def initialize(allows_invocation_now)
@allows_invocation_now = allows_invocation_now
end
-
+
def allows_invocation_now?
@allows_invocation_now
end
-
+
end
-
+
def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
constraint_one = FakeConstraint.new(allows_invocation_now = true)
constraint_two = FakeConstraint.new(allows_invocation_now = true)
@@ -391,7 +391,7 @@ def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation
expectation.add_ordering_constraint(constraint_two)
assert expectation.in_correct_order?
end
-
+
def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
constraint_one = FakeConstraint.new(allows_invocation_now = true)
constraint_two = FakeConstraint.new(allows_invocation_now = false)
@@ -400,7 +400,7 @@ def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allo
expectation.add_ordering_constraint(constraint_two)
assert !expectation.in_correct_order?
end
-
+
def test_should_match_if_all_ordering_constraints_allow_invocation_now
constraint_one = FakeConstraint.new(allows_invocation_now = true)
constraint_two = FakeConstraint.new(allows_invocation_now = true)
@@ -441,21 +441,21 @@ def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
2.times { expectation.invoke([]) }
assert expectation.satisfied?
end
-
+
class FakeSequence
-
+
attr_reader :expectations
-
+
def initialize
@expectations = []
end
-
+
def constrain_as_next_in_sequence(expectation)
@expectations << expectation
end
-
+
end
-
+
def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
sequence_one = FakeSequence.new
sequence_two = FakeSequence.new
@@ -464,23 +464,23 @@ def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
assert_equal [expectation], sequence_one.expectations
assert_equal [expectation], sequence_two.expectations
end
-
+
class FakeState
-
+
def initialize
@active = false
end
-
+
def activate
@active = true
end
-
+
def active?
@active
end
-
+
end
-
+
def test_should_change_state_when_expectation_is_invoked
state = FakeState.new
expectation = Expectation.new(nil, :method_one)
@@ -490,14 +490,14 @@ def test_should_change_state_when_expectation_is_invoked
expectation.invoke([])
assert state.active?
end
-
+
def test_should_match_when_state_is_active
state = FakeState.new
expectation = Expectation.new(nil, :method_one)
expectation.when(state)
assert !expectation.match?(:method_one)
-
+
state.activate
assert expectation.match?(:method_one)
end
@@ -526,5 +526,5 @@ def test_should_expose_invocation_count
expectation.invocation_count = 3
assert_equal 3, expectation.invocation_count
end
-
+
end
View
80 test/unit/mock_test.rb
@@ -5,16 +5,16 @@
require 'simple_counter'
class MockTest < Test::Unit::TestCase
-
+
include Mocha
-
+
def test_should_set_single_expectation
mock = build_mock
mock.expects(:method1).returns(1)
assert_nothing_raised(ExpectationError) do
assert_equal 1, mock.method1
end
- end
+ end
def test_should_build_and_store_expectations
mock = build_mock
@@ -22,7 +22,7 @@ def test_should_build_and_store_expectations
assert_not_nil expectation
assert_equal [expectation], mock.expectations.to_a
end
-
+
def test_should_not_stub_everything_by_default
mock = build_mock
assert_equal false, mock.everything_stubbed
@@ -33,71 +33,71 @@ def test_should_stub_everything
mock.stub_everything
assert_equal true, mock.everything_stubbed
end
-
+
def test_should_be_able_to_extend_mock_object_with_module
mock = build_mock
assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
end
-
+
def test_should_be_equal
mock = build_mock
assert_equal true, mock.eql?(mock)
end
-
+
if RUBY_VERSION < '1.9'
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }
else
OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || m == :object_id }
end
-
+
def test_should_be_able_to_mock_standard_object_methods
mock = build_mock
OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
assert mock.__verified__?
end
-
+
def test_should_be_able_to_stub_standard_object_methods
mock = build_mock
OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) }
OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
end
-
+
def test_should_create_and_add_expectations
mock = build_mock
expectation1 = mock.expects(:method1)
expectation2 = mock.expects(:method2)
assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
end
-
+
def test_should_pass_backtrace_into_expectation
mock = build_mock
backtrace = Object.new
expectation = mock.expects(:method1, backtrace)
assert_equal backtrace, expectation.backtrace
end
-
+
def test_should_pass_backtrace_into_stub
mock = build_mock
backtrace = Object.new
stub = mock.stubs(:method1, backtrace)
assert_equal backtrace, stub.backtrace
end
-
+
def test_should_create_and_add_stubs
mock = build_mock
stub1 = mock.stubs(:method1)
stub2 = mock.stubs(:method2)
assert_equal [stub1, stub2].to_set, mock.expectations.to_set
end
-
+
def test_should_invoke_expectation_and_return_result
mock = build_mock
mock.expects(:my_method).returns(:result)
result = mock.my_method
assert_equal :result, result
end
-
+
def test_should_not_raise_error_if_stubbing_everything
mock = build_mock
mock.stub_everything
@@ -107,7 +107,7 @@ def test_should_not_raise_error_if_stubbing_everything
end
assert_nil result
end
-
+
def test_should_raise_assertion_error_for_unexpected_method_call
mock = build_mock
error = assert_raise(ExpectationError) do
@@ -118,7 +118,7 @@ def test_should_raise_assertion_error_for_unexpected_method_call
assert_match(/argument1/, error.message)
assert_match(/argument2/, error.message)
end
-
+
def test_should_not_verify_successfully_because_not_all_expectations_have_been_satisfied
mock = build_mock
mock.expects(:method1)
@@ -126,23 +126,23 @@ def test_should_not_verify_successfully_because_not_all_expectations_have_been_s
mock.method1
assert !mock.__verified__?
end
-
+
def test_should_increment_assertion_counter_for_every_verified_expectation
mock = build_mock
-
+
mock.expects(:method1)
mock.method1
-
+
mock.expects(:method2)
mock.method2
-
+
assertion_counter = SimpleCounter.new
-
+
mock.__verified__?(assertion_counter)
-
+
assert_equal 2, assertion_counter.count
end
-
+
def test_should_yield_supplied_parameters_to_block
mock = build_mock
parameters_for_yield = [1, 2, 3]
@@ -151,35 +151,35 @@ def test_should_yield_supplied_parameters_to_block
mock.method1() { |*parameters| yielded_parameters = parameters }
assert_equal parameters_for_yield, yielded_parameters
end
-
+
def test_should_set_up_multiple_expectations_with_return_values
mock = build_mock
mock.expects(:method1 => :result1, :method2 => :result2)
assert_equal :result1, mock.method1
assert_equal :result2, mock.method2
end
-
+
def test_should_set_up_multiple_stubs_with_return_values
mock = build_mock
mock.stubs(:method1 => :result1, :method2 => :result2)
assert_equal :result1, mock.method1
assert_equal :result2, mock.method2
end
-
+
def test_should_keep_returning_specified_value_for_stubs
mock = build_mock
mock.stubs(:method1).returns(1)
assert_equal 1, mock.method1
assert_equal 1, mock.method1
end
-
+
def test_should_keep_returning_specified_value_for_expects
mock = build_mock
mock.expects(:method1).times(2).returns(1)
assert_equal 1, mock.method1
assert_equal 1, mock.method1
end
-
+
def test_should_match_most_recent_call_to_expects
mock = build_mock
mock.expects(:method1).returns(0)
@@ -207,18 +207,18 @@ def test_should_match_most_recent_call_to_expects_or_stubs
mock.stubs(:method1).returns(1)
assert_equal 1, mock.method1
end
-
+
def test_should_respond_to_expected_method
mock = build_mock
mock.expects(:method1)
assert_equal true, mock.respond_to?(:method1)
end
-
+
def test_should_not_respond_to_unexpected_method
mock = build_mock
assert_equal false, mock.respond_to?(:method1)
end
-
+
def test_should_respond_to_methods_which_the_responder_does_responds_to
instance = Class.new do
define_method(:respond_to?) { |symbol| true }
@@ -227,7 +227,7 @@ def test_should_respond_to_methods_which_the_responder_does_responds_to
mock.responds_like(instance)
assert_equal true, mock.respond_to?(:invoked_method)
end
-
+
def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
@@ -236,18 +236,18 @@ def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
mock.responds_like(instance)
assert_equal false, mock.respond_to?(:invoked_method)
end
-
+
def test_should_return_itself_to_allow_method_chaining
mock = build_mock
assert_same mock.responds_like(Object.new), mock
end
-
+
def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
mock = build_mock
mock.stubs(:invoked_method)
assert_nothing_raised(NoMethodError) { mock.invoked_method }
end
-
+
def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |symbol| true }
@@ -257,7 +257,7 @@ def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_m
mock.stubs(:invoked_method)
assert_nothing_raised(NoMethodError) { mock.invoked_method }
end
-
+
def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
@@ -268,7 +268,7 @@ def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_m
mock.stubs(:invoked_method)
assert_raises(NoMethodError) { mock.invoked_method }
end
-
+
def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
instance = Class.new do
define_method(:respond_to?) { |symbol| false }
@@ -283,12 +283,12 @@ def test_should_raise_no_method_error_with_message_indicating_that_mock_is_const
assert_match(/which responds like mocha_inspect/, e.message)
end
end
-
+
def test_should_handle_respond_to_with_private_methods_param_without_error
mock = build_mock
assert_nothing_raised{ mock.respond_to?(:object_id, false) }
end
-
+
def test_should_respond_to_any_method_if_stubbing_everything
mock = build_mock
mock.stub_everything
View
48 test/unit/mockery_test.rb
@@ -4,28 +4,28 @@
require 'bourne/invocation'
class MockeryTest < Test::Unit::TestCase
-
+
include Mocha
-
+
def test_should_build_instance_of_mockery
mockery = Mockery.instance
assert_not_nil mockery
assert_kind_of Mockery, mockery
end
-
+
def test_should_cache_instance_of_mockery
mockery_1 = Mockery.instance
mockery_2 = Mockery.instance
assert_same mockery_1, mockery_2
end
-
+
def test_should_expire_mockery_instance_cache
mockery_1 = Mockery.instance
Mockery.reset_instance
mockery_2 = Mockery.instance
assert_not_same mockery_1, mockery_2
end
-
+
def test_should_raise_expectation_error_because_not_all_expectations_are_satisfied
mockery = Mockery.new
mock_1 = mockery.named_mock('mock-1') { expects(:method_1) }
@@ -34,20 +34,20 @@ def test_should_raise_expectation_error_because_not_all_expectations_are_satisfi
0.times { mock_2.method_2 }
assert_raises(ExpectationError) { mockery.verify }
end
-
+
def test_should_reset_list_of_mocks_on_teardown
mockery = Mockery.new
mock = mockery.unnamed_mock { expects(:my_method) }
mockery.teardown
assert_nothing_raised(ExpectationError) { mockery.verify }
end
-
+
def test_should_build_instance_of_stubba_on_instantiation
mockery = Mockery.new
assert_not_nil mockery.stubba
assert_kind_of Central, mockery.stubba
end
-
+
def test_should_build_new_instance_of_stubba_on_teardown
mockery = Mockery.new
stubba_1 = mockery.stubba
@@ -55,27 +55,27 @@ def test_should_build_new_instance_of_stubba_on_teardown
stubba_2 = mockery.stubba
assert_not_same stubba_1, stubba_2
end
-
+
def test_should_build_and_store_new_state_machine
mockery = Mockery.new
mockery.new_state_machine('state-machine-name')
assert_equal 1, mockery.state_machines.length
assert_kind_of StateMachine, mockery.state_machines[0]
end
-
+
def test_should_reset_list_of_state_machines_on_teardown
mockery = Mockery.new
mockery.new_state_machine('state-machine-name')
mockery.teardown
assert_equal 0, mockery.state_machines.length
end
-
+
class FakeMethod
def stub; end
def unstub; end
def matches?(other); true; end
end
-
+
def test_should_unstub_all_methods_on_teardown
mockery = Mockery.new
stubba = mockery.stubba
@@ -83,65 +83,65 @@ def test_should_unstub_all_methods_on_teardown
mockery.teardown
assert stubba.stubba_methods.empty?
end
-
+
def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
mockery = Mockery.new
mock = mockery.unnamed_mock
assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.mocha_inspect
end
-
+
def test_should_display_object_id_for_inspect_if_mock_has_no_name
mockery = Mockery.new
mock = mockery.unnamed_mock
assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.inspect
end
-
+
def test_should_display_name_for_mocha_inspect_if_mock_has_string_name
mockery = Mockery.new
mock = mockery.named_mock('named_mock')
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
end
-
+
def test_should_display_name_for_mocha_inspect_if_mock_has_symbol_name
mockery = Mockery.new
mock = mockery.named_mock(:named_mock)
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
end
-
+
def test_should_display_name_for_inspect_if_mock_has_string_name
mockery = Mockery.new
mock = mockery.named_mock('named_mock')
assert_equal "#<Mock:named_mock>", mock.inspect
end
-
+
def test_should_display_name_for_inspect_if_mock_has_symbol_name
mockery = Mockery.new
mock = mockery.named_mock(:named_mock)
assert_equal "#<Mock:named_mock>", mock.inspect
end
-
+
def test_should_display_impersonated_object_for_mocha_inspect
mockery = Mockery.new
instance = Object.new
mock = mockery.mock_impersonating(instance)
assert_equal "#{instance.mocha_inspect}", mock.mocha_inspect
end
-
+
def test_should_display_impersonated_object_for_inspect
mockery = Mockery.new
instance = Object.new
mock = mockery.mock_impersonating(instance)
assert_equal "#{instance.mocha_inspect}", mock.inspect
end
-
+
class FakeClass; end
-
+
def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_mocha_inspect
mockery = Mockery.new
mock = mockery.mock_impersonating_any_instance_of(FakeClass)
assert_equal "#<AnyInstance:MockeryTest::FakeClass>", mock.mocha_inspect
end
-
+
def test_should_display_any_instance_prefix_followed_by_class_whose_instances_are_being_impersonated_for_inspect
mockery = Mockery.new
mock = mockery.mock_impersonating_any_instance_of(FakeClass)
@@ -160,5 +160,5 @@ def test_should_record_invocation
assert_equal method, invocation.method_name
assert_equal args, invocation.arguments
end
-
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.