test/mocha/mock_methods_test.rb in mocha-0.2.1 vs test/mocha/mock_methods_test.rb in mocha-0.3.0
- old
+ new
@@ -14,10 +14,26 @@
expectation2 = mock.expects(:method2)
assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
end
+ def test_should_pass_backtrace_into_expectation
+ mock = Object.new
+ mock.extend(MockMethods)
+ backtrace = Object.new
+ expectation = mock.expects(:method1, backtrace)
+ assert_equal backtrace, expectation.backtrace
+ end
+
+ def test_should_pass_backtrace_into_stub
+ mock = Object.new
+ mock.extend(MockMethods)
+ backtrace = Object.new
+ stub = mock.stubs(:method1, backtrace)
+ assert_equal backtrace, stub.backtrace
+ end
+
def test_should_create_and_add_stubs
mock = Object.new
mock.extend(MockMethods)
stub1 = mock.stubs(:method1)
@@ -44,10 +60,21 @@
result = mock.my_method
assert_equal :result, result
end
+ def test_should_not_raise_error_if_stubbing_everything
+ mock = Class.new { def initialize; @stub_everything = true; end }.new
+ mock.extend(MockMethods)
+
+ result = nil
+ assert_nothing_raised(Test::Unit::AssertionFailedError) do
+ result = mock.unexpected_method
+ end
+ assert_nil result
+ end
+
def test_should_raise_no_method_error
mock = Object.new
mock.extend(MockMethods)
assert_raise(NoMethodError) do
mock.super_method_missing(nil)
@@ -105,37 +132,46 @@
assert_raise(Test::Unit::AssertionFailedError) do
mock.verify
end
end
- def test_should_only_verify_expectations_matching_method_name
+ def test_should_report_possible_expectations
+ mock = Object.new.extend(MockMethods)
+ mock.expects(:meth).with(1)
+ exception = assert_raise(Test::Unit::AssertionFailedError) { mock.meth(2) }
+ assert_equal "Unexpected message :meth(2)\nSimilar expectations :meth(1)", exception.message
+ end
+
+ def test_should_pass_block_through_to_expectations_verify_method
mock = Object.new
mock.extend(MockMethods)
- mock.expects(:method1)
- mock.expects(:method2)
+ expected_expectation = mock.expects(:method1)
mock.method1
- assert_nothing_raised(Test::Unit::AssertionFailedError) do
- mock.verify(:method1)
- end
- assert_raise(Test::Unit::AssertionFailedError) do
- mock.verify(:method2)
- end
+ expectations = []
+ mock.verify() { |expectation| expectations << expectation }
+ assert_equal [expected_expectation], expectations
end
- def test_should_only_verify_expectations_matching_multiple_method_names
+ def test_should_yield_supplied_parameters_to_block
mock = Object.new
mock.extend(MockMethods)
+ parameters_for_yield = [1, 2, 3]
+ mock.expects(:method1).yields(*parameters_for_yield)
+ yielded_parameters = nil
+ mock.method1() { |*parameters| yielded_parameters = parameters }
+ assert_equal parameters_for_yield, yielded_parameters
+ end
+
+ def test_should_respond_to_expected_method
+ mock = Object.new
+ mock.extend(MockMethods)
mock.expects(:method1)
- mock.expects(:method2)
- assert_raise(Test::Unit::AssertionFailedError) do
- mock.verify(:method1, :method2)
- end
+ assert_equal true, mock.respond_to?(:method1)
end
- def test_should_report_possible_expectations
- mock = Object.new.extend(MockMethods)
- mock.expects(:meth).with(1)
- exception = assert_raise(Test::Unit::AssertionFailedError) { mock.meth(2) }
- assert_equal "Unexpected message :meth(2)\nSimilar expectations :meth(1)", exception.message
+ def test_should_not_respond_to_unexpected_method
+ mock = Object.new
+ mock.extend(MockMethods)
+ assert_equal false, mock.respond_to?(:method1)
end
end
\ No newline at end of file