mocha 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'mocha/mock'
3
+ require 'method_definer'
3
4
 
4
5
  require 'stubba/object'
5
6
 
@@ -31,23 +32,53 @@ class ObjectTest < Test::Unit::TestCase
31
32
  $stubba = Mock.new
32
33
  $stubba.expects(:stub).with(Stubba::InstanceMethod.new(instance, :method1))
33
34
  instance.expects(:method1)
34
- $stubba.verify(:stub)
35
+ $stubba.verify
35
36
  end
36
37
 
37
38
  def test_should_build_and_store_expectation
38
39
  instance = Object.new
39
- $stubba = Mock.new(:stub => nil)
40
+ $stubba = Mock.new
41
+ $stubba.stubs(:stub)
40
42
  expectation = instance.expects(:method1)
41
43
  assert_equal [expectation], instance.mocha.expectations
42
44
  end
43
45
 
44
46
  def test_should_verify_expectations
45
47
  instance = Object.new
46
- $stubba = Mock.new(:stub => nil)
48
+ $stubba = Mock.new
49
+ $stubba.stubs(:stub)
47
50
  instance.expects(:method1).with(:value1, :value2)
48
51
  assert_raise(Test::Unit::AssertionFailedError) { instance.verify }
49
52
  end
50
53
 
54
+ def test_should_pass_backtrace_into_expects
55
+ instance = Object.new
56
+ $stubba = Mock.new
57
+ $stubba.stubs(:stub)
58
+ mocha = Object.new
59
+ mocha.define_instance_accessor(:expects_parameters)
60
+ mocha.define_instance_method(:expects) { |*parameters| self.expects_parameters = parameters }
61
+ backtrace = Object.new
62
+ instance.define_instance_method(:mocha) { mocha }
63
+ instance.define_instance_method(:caller) { backtrace }
64
+ instance.expects(:method1)
65
+ assert_equal [:method1, backtrace], mocha.expects_parameters
66
+ end
67
+
68
+ def test_should_pass_backtrace_into_stubs
69
+ instance = Object.new
70
+ $stubba = Mock.new
71
+ $stubba.stubs(:stub)
72
+ mocha = Object.new
73
+ mocha.define_instance_accessor(:stubs_parameters)
74
+ mocha.define_instance_method(:stubs) { |*parameters| self.stubs_parameters = parameters }
75
+ backtrace = Object.new
76
+ instance.define_instance_method(:mocha) { mocha }
77
+ instance.define_instance_method(:caller) { backtrace }
78
+ instance.stubs(:method1)
79
+ assert_equal [:method1, backtrace], mocha.stubs_parameters
80
+ end
81
+
51
82
  def test_should_build_any_instance_object
52
83
  klass = Class.new
53
84
  any_instance = klass.any_instance
@@ -67,12 +98,13 @@ class ObjectTest < Test::Unit::TestCase
67
98
  $stubba = Mock.new
68
99
  $stubba.expects(:stub).with(Stubba::ClassMethod.new(klass, :method1))
69
100
  klass.expects(:method1)
70
- $stubba.verify(:stub)
101
+ $stubba.verify
71
102
  end
72
103
 
73
104
  def test_should_build_and_store_class_method_expectation
74
105
  klass = Class.new
75
- $stubba = Mock.new(:stub => nil)
106
+ $stubba = Mock.new
107
+ $stubba.stubs(:stub)
76
108
  expectation = klass.expects(:method1)
77
109
  assert_equal [expectation], klass.mocha.expectations
78
110
  end
@@ -82,12 +114,13 @@ class ObjectTest < Test::Unit::TestCase
82
114
  $stubba = Mock.new
83
115
  $stubba.expects(:stub).with(Stubba::ClassMethod.new(mod, :method1))
84
116
  mod.expects(:method1)
85
- $stubba.verify(:stub)
117
+ $stubba.verify
86
118
  end
87
119
 
88
120
  def test_should_build_and_store_module_method_expectation
89
121
  mod = Module.new
90
- $stubba = Mock.new(:stub => nil)
122
+ $stubba = Mock.new
123
+ $stubba.stubs(:stub)
91
124
  expectation = mod.expects(:method1)
92
125
  assert_equal [expectation], mod.mocha.expectations
93
126
  end
@@ -128,5 +161,5 @@ class ObjectTest < Test::Unit::TestCase
128
161
  any_instance = Class::AnyInstance.new(klass)
129
162
  assert_equal klass, any_instance.stubba_object
130
163
  end
131
-
164
+
132
165
  end
@@ -41,9 +41,34 @@ class SetupAndTeardownTest < Test::Unit::TestCase
41
41
  assert $stubba.is_a?(Stubba::Central)
42
42
  end
43
43
 
44
+ def test_should_verify_all_expectations
45
+ test_case = stubbed_test_case_class.new
46
+ stubba = Mock.new
47
+ stubba.expects(:verify_all)
48
+ stubba.stubs(:unstub_all)
49
+ $stubba = stubba
50
+
51
+ test_case.teardown_stubs
52
+
53
+ stubba.verify
54
+ end
55
+
56
+ def test_should_pass_block_to_add_assertion_to_test_case
57
+ test_case = stubbed_test_case_class.new
58
+ $stubba = Mock.new
59
+ $stubba.stubs(:verify_all).yields
60
+ $stubba.stubs(:unstub_all)
61
+
62
+ test_case.teardown_stubs
63
+
64
+ assert_equal true, test_case.add_assertion_called
65
+ end
66
+
44
67
  def test_should_unstub_all_stubbed_methods
45
68
  test_case = stubbed_test_case_class.new
46
- stubba = Mock.new(:unstub_all => nil)
69
+ stubba = Mock.new
70
+ stubba.stubs(:verify_all)
71
+ stubba.expects(:unstub_all)
47
72
  $stubba = stubba
48
73
 
49
74
  test_case.teardown_stubs
@@ -51,15 +76,46 @@ class SetupAndTeardownTest < Test::Unit::TestCase
51
76
  stubba.verify
52
77
  end
53
78
 
79
+ def test_should_unstub_all_stubbed_methods_even_if_verify_all_raises_exception
80
+ test_case = stubbed_test_case_class.new
81
+ stubba = Mock.new
82
+ stubba.stubs(:verify_all).raises(Exception)
83
+ stubba.expects(:unstub_all)
84
+ $stubba = stubba
85
+
86
+ assert_raises(Exception) { test_case.teardown_stubs }
87
+
88
+ stubba.verify
89
+ end
90
+
54
91
  def test_should_set_stubba_to_nil
55
92
  test_case = stubbed_test_case_class.new
56
- $stubba = Mock.new(:unstub_all => nil)
93
+ $stubba = Mock.new
94
+ $stubba.stubs(:verify_all)
95
+ $stubba.stubs(:unstub_all)
57
96
 
58
97
  test_case.teardown_stubs
59
98
 
60
99
  assert_nil $stubba
61
100
  end
62
101
 
102
+ def test_should_set_stubba_to_nil_even_if_verify_all_raises_exception
103
+ test_case = stubbed_test_case_class.new
104
+ $stubba = Mock.new
105
+ $stubba.stubs(:verify_all).raises(Exception)
106
+ $stubba.stubs(:unstub_all)
107
+
108
+ assert_raises(Exception) { test_case.teardown_stubs }
109
+
110
+ assert_nil $stubba
111
+ end
112
+
113
+ def test_should_not_raise_exception_if_no_stubba_central_available
114
+ test_case = stubbed_test_case_class.new
115
+ $stubba = nil
116
+ assert_nothing_raised { test_case.teardown_stubs }
117
+ end
118
+
63
119
  private
64
120
 
65
121
  def stubbed_test_case_class
@@ -68,6 +124,8 @@ class SetupAndTeardownTest < Test::Unit::TestCase
68
124
  define_method(:add_setup_method) {}
69
125
  define_method(:add_teardown_method) {}
70
126
  end
127
+ attr_accessor :add_assertion_called
128
+ define_method(:add_assertion) { self.add_assertion_called = true }
71
129
  include SetupAndTeardown
72
130
  end
73
131
  end
@@ -35,7 +35,6 @@ class StubbaAcceptanceTest < Test::Unit::TestCase
35
35
  widget = Widget.new
36
36
  widget.expects(:model).returns('different_model')
37
37
  assert_equal 'different_model', widget.model
38
- widget.verify
39
38
  end
40
39
 
41
40
  def test_should_stub_module_method
@@ -75,14 +74,12 @@ class StubbaAcceptanceTest < Test::Unit::TestCase
75
74
  def should_stub_module_method
76
75
  Thingy.expects(:wotsit).returns(:dooda)
77
76
  assert_equal :dooda, Thingy.wotsit
78
- Thingy.verify
79
77
  end
80
78
 
81
79
  def should_stub_class_method
82
80
  widgets = [Widget.new]
83
81
  Widget.expects(:find).with(:all).returns(widgets)
84
82
  assert_equal widgets, Widget.find(:all)
85
- Widget.verify
86
83
  end
87
84
 
88
85
  def should_stub_two_different_class_methods
@@ -92,7 +89,6 @@ class StubbaAcceptanceTest < Test::Unit::TestCase
92
89
  Widget.expects(:create).with(:model => 'wombat').returns(created_widget)
93
90
  assert_equal found_widgets, Widget.find(:all)
94
91
  assert_equal created_widget, Widget.create(:model => 'wombat')
95
- Widget.verify
96
92
  end
97
93
 
98
94
  def should_stub_instance_method_on_any_instance_of_a_class
@@ -101,7 +97,6 @@ class StubbaAcceptanceTest < Test::Unit::TestCase
101
97
  widget_2 = Widget.new
102
98
  assert_equal 'another_model', widget_1.model
103
99
  assert_equal 'another_model', widget_2.model
104
- Widget.any_instance.verify
105
100
  end
106
101
 
107
102
  end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+ require 'stubba/object'
3
+ require 'smart_test_case/multiple_setup_and_teardown'
4
+ require 'stubba/setup_and_teardown'
5
+ require 'shared/backtracefilter'
6
+ require 'execution_point'
7
+
8
+ class StubbaTestResultIntegrationTest < Test::Unit::TestCase
9
+
10
+ def test_should_include_expectation_verification_in_assertion_count
11
+ test_result = run_test do
12
+ object = Class.new { def message; end }.new
13
+ object.expects(:message)
14
+ object.message
15
+ end
16
+ assert_equal 1, test_result.assertion_count
17
+ end
18
+
19
+ def test_should_include_assertions_in_assertion_count
20
+ test_result = run_test do
21
+ assert true
22
+ end
23
+ assert_equal 1, test_result.assertion_count
24
+ end
25
+
26
+ def test_should_not_include_stubbing_expectation_verification_in_assertion_count
27
+ test_result = run_test do
28
+ object = Class.new { def message; end }.new
29
+ object.stubs(:message)
30
+ object.message
31
+ end
32
+ assert_equal 0, test_result.assertion_count
33
+ end
34
+
35
+ def test_should_include_expectation_verification_failure_in_failure_count
36
+ test_result = run_test do
37
+ object = Class.new { def message; end }.new
38
+ object.expects(:message)
39
+ end
40
+ assert_equal 1, test_result.failure_count
41
+ end
42
+
43
+ def test_should_include_assertion_failure_in_failure_count
44
+ test_result = run_test do
45
+ flunk
46
+ end
47
+ assert_equal 1, test_result.failure_count
48
+ end
49
+
50
+ def test_should_display_backtrace_indicating_line_number_where_expects_was_called
51
+ test_result = Test::Unit::TestResult.new
52
+ faults = []
53
+ test_result.add_listener(Test::Unit::TestResult::FAULT, &lambda { |fault| faults << fault })
54
+ execution_point = nil
55
+ run_test(test_result) do
56
+ object = Class.new { def message; end }.new
57
+ execution_point = ExecutionPoint.current; object.expects(:message)
58
+ end
59
+ assert_equal 1, faults.size
60
+ assert_equal execution_point, ExecutionPoint.new(faults.first.location)
61
+ end
62
+
63
+ def test_should_display_backtrace_indicating_line_number_where_failing_assertion_was_called
64
+ test_result = Test::Unit::TestResult.new
65
+ faults = []
66
+ test_result.add_listener(Test::Unit::TestResult::FAULT, &lambda { |fault| faults << fault })
67
+ execution_point = nil
68
+ run_test(test_result) do
69
+ execution_point = ExecutionPoint.current; flunk
70
+ end
71
+ assert_equal 1, faults.size
72
+ assert_equal execution_point, ExecutionPoint.new(faults.first.location)
73
+ end
74
+
75
+ def run_test(test_result = Test::Unit::TestResult.new, &block)
76
+ test_class = Class.new(Test::Unit::TestCase) do
77
+ include MultipleSetupAndTeardown
78
+ include SetupAndTeardown
79
+ define_method(:test_me, &block)
80
+ end
81
+ test = test_class.new(:test_me)
82
+ test.run(test_result) {}
83
+ test_result
84
+ end
85
+
86
+ end
@@ -1,4 +1,4 @@
1
- $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
2
- $:.unshift File.join(File.dirname(__FILE__))
1
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__)))
3
3
 
4
4
  require 'test/unit'
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: mocha
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2006-08-14 00:00:00 +01:00
6
+ version: 0.3.0
7
+ date: 2006-08-24 00:00:00 +01:00
8
8
  summary: Mocking and stubbing library
9
9
  require_paths:
10
10
  - lib
@@ -46,6 +46,7 @@ files:
46
46
  - lib/mocha/mock.rb
47
47
  - lib/mocha/mock_methods.rb
48
48
  - lib/mocha/pretty_parameters.rb
49
+ - lib/shared/backtracefilter.rb
49
50
  - lib/smart_test_case/multiple_setup_and_teardown.rb
50
51
  - lib/stubba/any_instance_method.rb
51
52
  - lib/stubba/central.rb
@@ -56,10 +57,13 @@ files:
56
57
  - test/active_record_test_case.rb
57
58
  - test/all_tests.rb
58
59
  - test/auto_mock_acceptance_test.rb
60
+ - test/execution_point.rb
59
61
  - test/method_definer.rb
60
62
  - test/mocha_acceptance_test.rb
63
+ - test/mocha_test_result_integration_test.rb
61
64
  - test/stubba_acceptance_test.rb
62
65
  - test/stubba_integration_test.rb
66
+ - test/stubba_test_result_integration_test.rb
63
67
  - test/test_helper.rb
64
68
  - test/auto_mocha/auto_mock_test.rb
65
69
  - test/auto_mocha/mock_class_test.rb