mocha 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,3 @@
1
- require 'test_helper'
2
- require 'stubba/test_case' # only works if loaded before any derived test cases
3
-
4
1
  require 'test/unit/ui/console/testrunner'
5
2
 
6
3
  require 'mocha/inspect_test'
@@ -9,92 +6,74 @@ require 'mocha/expectation_test'
9
6
  require 'mocha/infinite_range_test'
10
7
  require 'mocha/mock_methods_test'
11
8
  require 'mocha/mock_test'
12
-
13
- class MochaUnitTests
14
-
15
- def self.suite
16
- suite = Test::Unit::TestSuite.new('MochaUnitTests')
17
- suite << InspectTest.suite
18
- suite << PrettyParametersTest.suite
19
- suite << ExpectationTest.suite
20
- suite << InfiniteRangeTest.suite
21
- suite << MockMethodsTest.suite
22
- suite << MockTest.suite
23
- suite
24
- end
25
-
26
- end
27
-
28
- Test::Unit::UI::Console::TestRunner.run(MochaUnitTests)
9
+ require 'mocha/auto_verify_test'
29
10
 
30
11
  require 'auto_mocha/mock_class_test'
31
12
  require 'auto_mocha/auto_mock_test'
32
13
 
33
- class AutoMochaUnitTests
34
-
35
- def self.suite
36
- suite = Test::Unit::TestSuite.new('AutoMochaUnitTests')
37
- suite << AutoMockTest.suite
38
- suite << MockClassTest.suite
39
- end
40
-
41
- end
42
-
43
- Test::Unit::UI::Console::TestRunner.run(AutoMochaUnitTests)
44
-
45
- require 'stubba/stubba_test'
14
+ require 'stubba/central_test'
46
15
  require 'stubba/class_method_test'
47
16
  require 'stubba/instance_method_test'
48
17
  require 'stubba/any_instance_method_test'
49
- require 'stubba/test_case_test'
18
+ require 'stubba/setup_and_teardown_test'
19
+ require 'stubba/object_test'
20
+
21
+ require 'smart_test_case/multiple_setup_and_teardown_test'
50
22
 
51
- class StubbaUnitTests
23
+ class UnitTests
52
24
 
53
25
  def self.suite
54
- suite = Test::Unit::TestSuite.new('StubbaUnitTests')
55
- suite << StubbaTest.suite
26
+ suite = Test::Unit::TestSuite.new('UnitTests')
27
+ suite << InspectTest.suite
28
+ suite << PrettyParametersTest.suite
29
+ suite << ExpectationTest.suite
30
+ suite << InfiniteRangeTest.suite
31
+ suite << MockMethodsTest.suite
32
+ suite << MockTest.suite
33
+ suite << AutoVerifyTest.suite
34
+ suite << MockClassTest.suite
35
+ suite << AutoMockTest.suite
36
+ suite << CentralTest.suite
56
37
  suite << ClassMethodTest.suite
57
38
  suite << InstanceMethodTest.suite
58
39
  suite << AnyInstanceMethodTest.suite
59
- suite << TestCaseTest.suite
40
+ suite << SetupAndTeardownTest.suite
41
+ suite << ObjectTest.suite
42
+ suite << MultipleSetupAndTeardownTest.suite
60
43
  suite
61
44
  end
62
45
 
63
46
  end
64
47
 
65
- Test::Unit::UI::Console::TestRunner.run(StubbaUnitTests)
48
+ Test::Unit::UI::Console::TestRunner.run(UnitTests)
66
49
 
67
- require 'stubba/object_test'
50
+ require 'stubba_integration_test'
68
51
 
69
- class IsolatedStubbaUnitTests # avoid loading stubba_object until now which breaks other stubba unit tests
52
+ class IntegrationTests
70
53
 
71
54
  def self.suite
72
- suite = Test::Unit::TestSuite.new('IsolatedStubbaUnitTests')
73
- suite << ObjectTest.suite
74
- suite
55
+ suite = Test::Unit::TestSuite.new('IntegrationTests')
56
+ suite << StubbaIntegrationTest.suite
75
57
  end
76
58
 
77
59
  end
78
60
 
79
- Test::Unit::UI::Console::TestRunner.run(IsolatedStubbaUnitTests)
80
-
81
- require 'stubba_integration_test'
82
- Test::Unit::UI::Console::TestRunner.run(StubbaIntegrationTest)
61
+ Test::Unit::UI::Console::TestRunner.run(IntegrationTests)
83
62
 
84
63
  require 'mocha_acceptance_test'
85
- require 'stubba_acceptance_test'
86
64
  require 'auto_mock_acceptance_test'
65
+ require 'stubba_acceptance_test'
87
66
 
88
67
  class AcceptanceTests
89
68
 
90
69
  def self.suite
91
70
  suite = Test::Unit::TestSuite.new('AcceptanceTests')
92
71
  suite << MochaAcceptanceTest.suite
93
- suite << StubbaAcceptanceTest.suite
94
72
  suite << AutoMockAcceptanceTest.suite
73
+ suite << StubbaAcceptanceTest.suite
95
74
  suite
96
75
  end
97
76
 
98
77
  end
99
78
 
100
- Test::Unit::UI::Console::TestRunner.run(AcceptanceTests)
79
+ Test::Unit::UI::Console::TestRunner.run(AcceptanceTests)
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'mocha'
3
2
  require 'auto_mocha'
4
3
 
5
4
  class Product < ActiveRecord::Base
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/auto_verify'
3
+ require 'method_definer'
4
+
5
+ class AutoVerifyTest < Test::Unit::TestCase
6
+
7
+ attr_reader :test_case
8
+
9
+ def setup
10
+ @test_case = Object.new
11
+ class << test_case
12
+ def self.add_teardown_method(symbol); end
13
+ include AutoVerify
14
+ end
15
+ end
16
+
17
+ def test_should_build_mock
18
+ assert test_case.mock.is_a?(Mocha::Mock)
19
+ end
20
+
21
+ def test_should_build_new_mock_each_time
22
+ assert_not_equal test_case.mock, test_case.mock
23
+ end
24
+
25
+ def test_should_store_each_new_mock
26
+ expected = Array.new(3) { test_case.mock }
27
+ assert_equal expected, test_case.mocks
28
+ end
29
+
30
+ def test_should_verify_each_mock_on_teardown
31
+ mocks = Array.new(3) do
32
+ mock = Object.new
33
+ mock.define_instance_accessor(:verify_called)
34
+ mock.define_instance_method(:verify) { self.verify_called = true }
35
+ mock
36
+ end
37
+ test_case.replace_instance_method(:mocks) { mocks }
38
+ test_case.teardown_mocks
39
+ assert mocks.all? { |mock| mock.verify_called }
40
+ end
41
+
42
+ def test_should_reset_mocks_on_teardown
43
+ mock = Class.new { define_method(:verify) {} }.new
44
+ test_case.mocks << mock
45
+ test_case.teardown_mocks
46
+ assert test_case.mocks.empty?
47
+ end
48
+
49
+ end
@@ -35,11 +35,9 @@ class MochaAcceptanceTest < Test::Unit::TestCase
35
35
 
36
36
  end
37
37
 
38
- include Mocha
39
-
40
38
  def test_should_move_both_tracks_forward_ten_steps
41
- left_track = Mock.new
42
- right_track = Mock.new
39
+ left_track = mock()
40
+ right_track = mock()
43
41
  steps_per_metre = 5
44
42
  rover = Rover.new(left_track, right_track, steps_per_metre, nil)
45
43
 
@@ -50,14 +48,11 @@ class MochaAcceptanceTest < Test::Unit::TestCase
50
48
  right_track.stubs(:moving?).returns(false)
51
49
 
52
50
  rover.forward(2)
53
-
54
- left_track.verify
55
- right_track.verify
56
51
  end
57
52
 
58
53
  def test_should_move_both_tracks_backward_ten_steps
59
- left_track = Mock.new
60
- right_track = Mock.new
54
+ left_track = mock()
55
+ right_track = mock()
61
56
  steps_per_metre = 5
62
57
  rover = Rover.new(left_track, right_track, steps_per_metre, nil)
63
58
 
@@ -68,14 +63,11 @@ class MochaAcceptanceTest < Test::Unit::TestCase
68
63
  right_track.stubs(:moving?).returns(false)
69
64
 
70
65
  rover.backward(2)
71
-
72
- left_track.verify
73
- right_track.verify
74
66
  end
75
67
 
76
68
  def test_should_move_left_track_forwards_five_steps_and_right_track_backwards_five_steps
77
- left_track = Mock.new
78
- right_track = Mock.new
69
+ left_track = mock()
70
+ right_track = mock()
79
71
  steps_per_degree = 5.0 / 90.0
80
72
  rover = Rover.new(left_track, right_track, nil, steps_per_degree)
81
73
 
@@ -86,14 +78,11 @@ class MochaAcceptanceTest < Test::Unit::TestCase
86
78
  right_track.stubs(:moving?).returns(false)
87
79
 
88
80
  rover.right(90)
89
-
90
- left_track.verify
91
- right_track.verify
92
81
  end
93
82
 
94
83
  def test_should_move_left_track_backwards_five_steps_and_right_track_forwards_five_steps
95
- left_track = Mock.new
96
- right_track = Mock.new
84
+ left_track = mock()
85
+ right_track = mock()
97
86
  steps_per_degree = 5.0 / 90.0
98
87
  rover = Rover.new(left_track, right_track, nil, steps_per_degree)
99
88
 
@@ -104,9 +93,6 @@ class MochaAcceptanceTest < Test::Unit::TestCase
104
93
  right_track.stubs(:moving?).returns(false)
105
94
 
106
95
  rover.left(90)
107
-
108
- left_track.verify
109
- right_track.verify
110
96
  end
111
97
 
112
98
  end
@@ -0,0 +1,91 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'smart_test_case/multiple_setup_and_teardown'
3
+ require 'active_record_test_case'
4
+
5
+ class MultipleSetupAndTeardownTest < Test::Unit::TestCase
6
+
7
+ def test_1
8
+ test_case = Class.new(Test::Unit::TestCase) do
9
+ define_method(:methods_called) { @methods_called ||= [] }
10
+ include MultipleSetupAndTeardown
11
+ add_setup_method(:setup_1)
12
+ define_method(:setup_1) { methods_called << :setup_1 }
13
+ define_method(:test_me) { methods_called << :test_me }
14
+ end
15
+ test = test_case.new(:test_me)
16
+ test_result = Test::Unit::TestResult.new
17
+ test.run(test_result) {}
18
+ assert test_result.passed?
19
+ assert_equal [:setup_1, :test_me], test.methods_called
20
+ end
21
+
22
+ def test_2
23
+ test_case = Class.new(Test::Unit::TestCase) do
24
+ define_method(:methods_called) { @methods_called ||= [] }
25
+ define_method(:setup) { methods_called << :setup }
26
+ include MultipleSetupAndTeardown
27
+ add_setup_method(:setup_1)
28
+ define_method(:setup_1) { methods_called << :setup_1 }
29
+ define_method(:test_me) { methods_called << :test_me }
30
+ end
31
+ test = test_case.new(:test_me)
32
+ test_result = Test::Unit::TestResult.new
33
+ test.run(test_result) {}
34
+ assert test_result.passed?
35
+ assert_equal [:setup, :setup_1, :test_me], test.methods_called
36
+ end
37
+
38
+ def test_3
39
+ test_case = Class.new(Test::Unit::TestCase) do
40
+ define_method(:methods_called) { @methods_called ||= [] }
41
+ include MultipleSetupAndTeardown
42
+ define_method(:setup) { methods_called << :setup }
43
+ add_setup_method(:setup_1)
44
+ define_method(:setup_1) { methods_called << :setup_1 }
45
+ define_method(:test_me) { methods_called << :test_me }
46
+ end
47
+ test = test_case.new(:test_me)
48
+ test_result = Test::Unit::TestResult.new
49
+ test.run(test_result) {}
50
+ assert test_result.passed?
51
+ assert_equal [:setup_1, :setup, :test_me], test.methods_called
52
+ end
53
+
54
+ def test_4
55
+ test_case = Class.new(Test::Unit::TestCase) do
56
+ define_method(:methods_called) { @methods_called ||= [] }
57
+ include MultipleSetupAndTeardown
58
+ add_setup_method(:setup_1)
59
+ define_method(:setup_1) { methods_called << :setup_1 }
60
+ end
61
+ derived_test_case = Class.new(test_case) do
62
+ define_method(:setup) { methods_called << :setup }
63
+ define_method(:test_me) { methods_called << :test_me }
64
+ end
65
+ test = derived_test_case.new(:test_me)
66
+ test_result = Test::Unit::TestResult.new
67
+ test.run(test_result) {}
68
+ assert test_result.passed?
69
+ assert_equal [:setup_1, :setup, :test_me], test.methods_called
70
+ end
71
+
72
+ def test_5
73
+ test_case = Class.new(Test::Unit::TestCase) do
74
+ define_method(:methods_called) { @methods_called ||= [] }
75
+ include ActiveRecordTestCase
76
+ include MultipleSetupAndTeardown
77
+ add_setup_method(:setup_1)
78
+ define_method(:setup_1) { methods_called << :setup_1 }
79
+ end
80
+ derived_test_case = Class.new(test_case) do
81
+ define_method(:setup) { methods_called << :setup }
82
+ define_method(:test_me) { methods_called << :test_me }
83
+ end
84
+ test = derived_test_case.new(:test_me)
85
+ test_result = Test::Unit::TestResult.new
86
+ test.run(test_result) {}
87
+ assert test_result.passed?
88
+ assert_equal [:setup_with_fixtures, :setup_1, :setup, :test_me, :teardown_with_fixtures], test.methods_called
89
+ end
90
+
91
+ end
@@ -34,7 +34,8 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
34
34
  method = AnyInstanceMethod.new(klass, :method_x)
35
35
  mocha = Mock.new
36
36
  mocha.expects(:method_x).with(:param1, :param2).returns(:result)
37
- any_instance = Mock.new(:mocha => mocha)
37
+ any_instance = Object.new
38
+ any_instance.define_instance_method(:mocha) { mocha }
38
39
  klass.define_instance_method(:any_instance) { any_instance }
39
40
 
40
41
  method.define_new_method
@@ -99,7 +100,7 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
99
100
 
100
101
  def test_should_call_reset_mocha
101
102
  klass = Class.new { def method_x; end }
102
- any_instance = Mock.new(:reset_mocha => nil)
103
+ any_instance = Class.new { attr_accessor :mocha_was_reset; def reset_mocha; self.mocha_was_reset = true; end }.new
103
104
  klass.define_instance_method(:any_instance) { any_instance }
104
105
  method = AnyInstanceMethod.new(klass, :method_x)
105
106
  method.replace_instance_method(:remove_new_method) { }
@@ -107,7 +108,7 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
107
108
 
108
109
  method.unstub
109
110
 
110
- any_instance.verify(:reset_mocha)
111
+ assert any_instance.mocha_was_reset
111
112
  end
112
113
 
113
114
  end
@@ -1,14 +1,15 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
- require 'stubba/stubba'
2
+
3
+ require 'stubba/central'
3
4
  require 'mocha/mock'
4
5
 
5
- class StubbaTest < Test::Unit::TestCase
6
+ class CentralTest < Test::Unit::TestCase
6
7
 
7
- include Stubba
8
8
  include Mocha
9
+ include Stubba
9
10
 
10
11
  def test_should_start_with_empty_stubba_methods
11
- stubba = Stubba.new
12
+ stubba = Central.new
12
13
 
13
14
  assert_equal [], stubba.stubba_methods
14
15
  end
@@ -16,7 +17,7 @@ class StubbaTest < Test::Unit::TestCase
16
17
  def test_should_stub_method_if_not_already_stubbed
17
18
  method = Mock.new
18
19
  method.expects(:stub)
19
- stubba = Stubba.new
20
+ stubba = Central.new
20
21
 
21
22
  stubba.stub(method)
22
23
 
@@ -26,7 +27,7 @@ class StubbaTest < Test::Unit::TestCase
26
27
  def test_should_not_stub_method_if_already_stubbed
27
28
  method = Mock.new
28
29
  method.expects(:stub).times(0)
29
- stubba = Stubba.new
30
+ stubba = Central.new
30
31
  stubba_methods = Mock.new
31
32
  stubba_methods.stubs(:include?).with(method).returns(true)
32
33
  stubba.stubba_methods = stubba_methods
@@ -39,7 +40,7 @@ class StubbaTest < Test::Unit::TestCase
39
40
  def test_should_record_method
40
41
  method = Mock.new
41
42
  method.expects(:stub)
42
- stubba = Stubba.new
43
+ stubba = Central.new
43
44
 
44
45
  stubba.stub(method)
45
46
 
@@ -47,7 +48,7 @@ class StubbaTest < Test::Unit::TestCase
47
48
  end
48
49
 
49
50
  def test_should_unstub_all_methods
50
- stubba = Stubba.new
51
+ stubba = Central.new
51
52
  method_1 = Mock.new(:unstub => nil)
52
53
  method_2 = Mock.new(:unstub => nil)
53
54
  stubba.stubba_methods = [method_1, method_2]
@@ -7,7 +7,6 @@ require 'stubba/class_method'
7
7
  class ClassMethodTest < Test::Unit::TestCase
8
8
 
9
9
  include Stubba
10
- include Mocha
11
10
 
12
11
  def test_should_provide_hidden_version_of_method_name
13
12
  method = ClassMethod.new(nil, :original_method_name)
@@ -46,7 +45,7 @@ class ClassMethodTest < Test::Unit::TestCase
46
45
 
47
46
  def test_should_define_a_new_method_which_should_call_mocha_method_missing
48
47
  klass = Class.new { def self.method_x; end }
49
- mocha = Mock.new
48
+ mocha = Mocha::Mock.new
50
49
  klass.define_instance_method(:mocha) { mocha }
51
50
  mocha.expects(:method_x).with(:param1, :param2).returns(:result)
52
51
  method = ClassMethod.new(klass, :method_x)