test/mocha/expectation_test.rb in mocha-0.2.1 vs test/mocha/expectation_test.rb in mocha-0.3.0
- old
+ new
@@ -1,9 +1,9 @@
require File.join(File.dirname(__FILE__), "..", "test_helper")
require 'method_definer'
-
require 'mocha/expectation'
+require 'execution_point'
class ExpectationTest < Test::Unit::TestCase
include Mocha
@@ -55,10 +55,43 @@
def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
expectation = Expectation.new(:expected_method).with() {|x, y, z| x + y == z}
assert !expectation.match?(:expected_method, 1, 0, 3)
end
+ def test_should_store_provided_backtrace
+ backtrace = Object.new
+ expectation = Expectation.new(:expected_method, backtrace)
+ assert_equal backtrace, expectation.backtrace
+ end
+
+ def test_should_default_backtrace_to_caller
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
+ end
+
+ def test_should_not_yield
+ expectation = Expectation.new(:expected_method)
+ yielded = false
+ expectation.invoke() { yielded = true }
+ assert_equal false, yielded
+ end
+
+ def test_should_yield_no_parameters
+ expectation = Expectation.new(:expected_method).yields
+ yielded_parameters = nil
+ expectation.invoke() { |*parameters| yielded_parameters = parameters }
+ assert_equal Array.new, yielded_parameters
+ end
+
+ def test_should_yield_with_specified_parameters
+ parameters_for_yield = [1, 2, 3]
+ expectation = Expectation.new(:expected_method).yields(*parameters_for_yield)
+ yielded_parameters = nil
+ expectation.invoke() { |*parameters| yielded_parameters = parameters }
+ assert_equal parameters_for_yield, yielded_parameters
+ end
+
def test_should_return_specified_value
expectation = Expectation.new(:expected_method).returns(99)
assert_equal 99, expectation.invoke
end
@@ -149,9 +182,39 @@
expectation = Expectation.new(:expected_method).times(2)
3.times {expectation.invoke}
assert_raise(Test::Unit::AssertionFailedError) {
expectation.verify
}
+ end
+
+ def test_should_yield_self_to_block
+ expectation = Expectation.new(:expected_method)
+ expectation.invoke
+ yielded_expectation = nil
+ expectation.verify { |x| yielded_expectation = x }
+ assert_equal expectation, yielded_expectation
+ end
+
+ def test_should_yield_to_block_before_raising_exception
+ expectation = Expectation.new(:expected_method)
+ yielded = false
+ assert_raise(Test::Unit::AssertionFailedError) {
+ expectation.verify { |x| yielded = true }
+ }
+ assert yielded
+ end
+
+ def test_should_store_backtrace_from_point_where_expectation_was_created
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
+ end
+
+ def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
+ error = assert_raise(Test::Unit::AssertionFailedError) {
+ expectation.verify
+ }
+ assert_equal execution_point, ExecutionPoint.new(error.backtrace)
end
def test_should_display_expectation_message_in_exception_message
options = [:a, :b, {:c => 1, :d => 2}]
expectation = Expectation.new(:expected_method).with(*options)
\ No newline at end of file