test/unit/mock_test.rb in bourne-1.3.0 vs test/unit/mock_test.rb in bourne-1.3.2
- old
+ new
@@ -2,31 +2,31 @@
# - copy new code from https://raw.github.com/freerange/mocha/master/test/unit/mock_test.rb
# - keep: require 'bourne/mock'
# - keep: FakeExpectation test
require File.expand_path('../../test_helper', __FILE__)
require 'bourne/mock'
-require 'mocha/expectation_error'
+require 'mocha/expectation_error_factory'
require 'set'
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
+ mock = build_mock
+ mock.expects(:method1).returns(1)
+ assert_nothing_raised(ExpectationErrorFactory.exception_class) do
+ assert_equal 1, mock.method1
+ end
end
def test_should_build_and_store_expectations
- mock = build_mock
- expectation = mock.expects(:method1)
- assert_not_nil expectation
- assert_equal [expectation], mock.__expectations__.to_a
+ mock = build_mock
+ expectation = mock.expects(:method1)
+ 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
@@ -38,11 +38,11 @@
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) }
+ assert_nothing_raised(ExpectationErrorFactory.exception_class) { mock.extend(Module.new) }
end
def test_should_be_equal
mock = build_mock
assert_equal true, mock.eql?(mock)
@@ -104,19 +104,19 @@
def test_should_not_raise_error_if_stubbing_everything
mock = build_mock
mock.stub_everything
result = nil
- assert_nothing_raised(ExpectationError) do
+ assert_nothing_raised(ExpectationErrorFactory.exception_class) do
result = mock.unexpected_method
end
assert_nil result
end
def test_should_raise_assertion_error_for_unexpected_method_call
mock = build_mock
- error = assert_raise(ExpectationError) do
+ error = assert_raise(ExpectationErrorFactory.exception_class) do
mock.unexpected_method_called(:my_method, :argument1, :argument2)
end
assert_match(/unexpected invocation/, error.message)
assert_match(/my_method/, error.message)
assert_match(/argument1/, error.message)
@@ -216,10 +216,16 @@
mock = build_mock
mock.expects(:method1)
assert_equal true, mock.respond_to?(:method1)
end
+ def test_should_respond_to_expected_method_as_string
+ 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
@@ -239,15 +245,38 @@
mock = build_mock
mock.responds_like(instance)
assert_equal false, mock.respond_to?(:invoked_method)
end
- def test_should_return_itself_to_allow_method_chaining
+ def test_should_respond_to_methods_which_the_responder_instance_does_responds_to
+ klass = Class.new do
+ define_method(:respond_to?) { |symbol| true }
+ end
mock = build_mock
+ mock.responds_like_instance_of(klass)
+ assert_equal true, mock.respond_to?(:invoked_method)
+ end
+
+ def test_should_not_respond_to_methods_which_the_responder_instance_does_not_responds_to
+ klass = Class.new do
+ define_method(:respond_to?) { |symbol| false }
+ end
+ mock = build_mock
+ mock.responds_like_instance_of(klass)
+ assert_equal false, mock.respond_to?(:invoked_method)
+ end
+
+ def test_respond_like_should_return_itself_to_allow_method_chaining
+ mock = build_mock
assert_same mock.responds_like(Object.new), mock
end
+ def test_respond_like_instance_of_should_return_itself_to_allow_method_chaining
+ mock = build_mock
+ assert_same mock.responds_like_instance_of(Object), 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
@@ -288,11 +317,11 @@
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) }
+ 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
@@ -302,10 +331,10 @@
def test_should_remove_expectation_for_unstubbed_method
mock = build_mock
mock.expects(:method1)
mock.unstub(:method1)
- e = assert_raises(ExpectationError) { mock.method1 }
+ e = assert_raises(ExpectationErrorFactory.exception_class) { mock.method1 }
assert_match(/unexpected invocation/, e.message)
end
class FakeExpectation
attr_reader :args