mocha 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,16 @@ module Mocha
4
4
  class Mock
5
5
 
6
6
  include MockMethods
7
-
8
- def initialize(stub_everything = false)
7
+
8
+ attr_reader :__mock_name
9
+ def initialize(stub_everything = false, name = nil)
9
10
  @stub_everything = stub_everything
11
+ @__mock_name = name
12
+ end
13
+
14
+ alias :mocha_inspect_before_hijacked_by_named_mocks :mocha_inspect
15
+ def mocha_inspect
16
+ @__mock_name ? "#<Mock: '#{@__mock_name}'>" : mocha_inspect_before_hijacked_by_named_mocks
10
17
  end
11
18
 
12
19
  end
@@ -1,24 +1,38 @@
1
1
  require 'mocha/expectation'
2
2
 
3
3
  module Mocha
4
+ # Methods added to mock objects.
5
+ # These methods all return an expectation which can be further modified by methods on Mocha::Expectation.
4
6
  module MockMethods
5
7
 
8
+ # :stopdoc:
9
+
6
10
  attr_reader :stub_everything
7
11
 
8
12
  def expectations
9
13
  @expectations ||= []
10
14
  end
11
15
 
16
+ # :startdoc:
17
+
18
+ # :call-seq: expects(symbol) -> expectation
19
+ #
20
+ # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
12
21
  def expects(symbol, backtrace = nil)
13
22
  expectations << Expectation.new(symbol, backtrace)
14
23
  expectations.last
15
24
  end
16
25
 
26
+ # :call-seq: stubs(symbol) -> expectation
27
+ #
28
+ # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
17
29
  def stubs(symbol, backtrace = nil)
18
30
  expectations << Stub.new(symbol, backtrace)
19
31
  expectations.last
20
32
  end
21
33
 
34
+ # :stopdoc:
35
+
22
36
  def method_missing(symbol, *arguments, &block)
23
37
  matching_expectation = matching_expectation(symbol, *arguments)
24
38
  if matching_expectation then
@@ -43,13 +57,19 @@ module Mocha
43
57
  end
44
58
 
45
59
  def unexpected_method_called(symbol, *arguments)
46
- MissingExpectation.new(symbol, expectations).with(*arguments).verify
60
+ MissingExpectation.new(symbol, self, expectations).with(*arguments).verify
47
61
  end
48
62
 
49
63
  def matching_expectation(symbol, *arguments)
50
64
  expectations.detect { |expectation| expectation.match?(symbol, *arguments) }
51
65
  end
52
66
 
67
+ # :startdoc:
68
+
69
+ # :call-seq: verify
70
+ #
71
+ # Asserts that all expectations have been fulfilled.
72
+ # Called automatically at the end of a test for mock objects created by methods in Mocha::AutoVerify.
53
73
  def verify(&block)
54
74
  expectations.each { |expectation| expectation.verify(&block) }
55
75
  end
@@ -1,5 +1,5 @@
1
1
  require 'smart_test_case/multiple_setup_and_teardown'
2
2
 
3
3
  class Test::Unit::TestCase
4
- include MultipleSetupAndTeardown unless ancestors.include?(MultipleSetupAndTeardown)
4
+ include SmartTestCase::MultipleSetupAndTeardown unless ancestors.include?(SmartTestCase::MultipleSetupAndTeardown)
5
5
  end
@@ -1,123 +1,125 @@
1
- module MultipleSetupAndTeardown
1
+ module SmartTestCase
2
+ module MultipleSetupAndTeardown
2
3
 
3
- def self.included(base)
4
+ def self.included(base)
4
5
 
5
- base.class_eval do
6
+ base.class_eval do
6
7
 
7
- def self.method_added(symbol)
8
- # disable until later
9
- end
8
+ def self.method_added(symbol)
9
+ # disable until later
10
+ end
10
11
 
11
- if method_defined?(:setup) then
12
- alias_method :setup_original, :setup
13
- define_method(:setup_new) do
14
- begin
15
- setup_original
16
- ensure
12
+ if method_defined?(:setup) then
13
+ alias_method :setup_original, :setup
14
+ define_method(:setup_new) do
15
+ begin
16
+ setup_original
17
+ ensure
18
+ setup_mocha
19
+ end
20
+ end
21
+ else
22
+ define_method(:setup_new) do
17
23
  setup_mocha
18
24
  end
19
25
  end
20
- else
21
- define_method(:setup_new) do
22
- setup_mocha
23
- end
24
- end
25
- alias_method :setup, :setup_new
26
-
27
- if method_defined?(:teardown) then
28
- alias_method :teardown_original, :teardown
29
- define_method(:teardown_new) do
30
- begin
26
+ alias_method :setup, :setup_new
27
+
28
+ if method_defined?(:teardown) then
29
+ alias_method :teardown_original, :teardown
30
+ define_method(:teardown_new) do
31
+ begin
32
+ teardown_mocha
33
+ ensure
34
+ teardown_original
35
+ end
36
+ end
37
+ else
38
+ define_method(:teardown_new) do
31
39
  teardown_mocha
32
- ensure
33
- teardown_original
34
40
  end
35
41
  end
36
- else
37
- define_method(:teardown_new) do
38
- teardown_mocha
39
- end
40
- end
41
- alias_method :teardown, :teardown_new
42
-
43
- def self.method_added(method)
44
- case method
45
- when :setup
46
- unless method_defined?(:setup_added)
47
- alias_method :setup_added, :setup
48
- define_method(:setup) do
49
- begin
50
- setup_new
51
- ensure
52
- setup_added
42
+ alias_method :teardown, :teardown_new
43
+
44
+ def self.method_added(method)
45
+ case method
46
+ when :setup
47
+ unless method_defined?(:setup_added)
48
+ alias_method :setup_added, :setup
49
+ define_method(:setup) do
50
+ begin
51
+ setup_new
52
+ ensure
53
+ setup_added
54
+ end
53
55
  end
54
56
  end
55
- end
56
- when :teardown
57
- unless method_defined?(:teardown_added)
58
- alias_method :teardown_added, :teardown
59
- define_method(:teardown) do
60
- begin
61
- teardown_added
62
- ensure
63
- teardown_new
57
+ when :teardown
58
+ unless method_defined?(:teardown_added)
59
+ alias_method :teardown_added, :teardown
60
+ define_method(:teardown) do
61
+ begin
62
+ teardown_added
63
+ ensure
64
+ teardown_new
65
+ end
64
66
  end
65
67
  end
66
68
  end
67
69
  end
68
- end
69
70
 
70
- class << self
71
+ class << self
71
72
 
72
- def setup_methods
73
- @setup_methods ||= []
74
- end
73
+ def setup_methods
74
+ @setup_methods ||= []
75
+ end
75
76
 
76
- def teardown_methods
77
- @teardown_methods ||= []
78
- end
77
+ def teardown_methods
78
+ @teardown_methods ||= []
79
+ end
79
80
 
80
- def add_setup_method(symbol)
81
- setup_methods << symbol
82
- end
81
+ def add_setup_method(symbol)
82
+ setup_methods << symbol
83
+ end
83
84
 
84
- def add_teardown_method(symbol)
85
- teardown_methods << symbol
86
- end
85
+ def add_teardown_method(symbol)
86
+ teardown_methods << symbol
87
+ end
87
88
 
88
- private
89
+ private
89
90
 
90
- def inherited_with_setup_and_teardown_methods(child)
91
- inherited_without_setup_and_teardown_methods(child) if respond_to?(:inherited_without_setup_and_teardown_methods, true)
92
- child.instance_variable_set('@setup_methods', setup_methods.dup)
93
- child.instance_variable_set('@teardown_methods', teardown_methods.dup)
94
- end
91
+ def inherited_with_setup_and_teardown_methods(child)
92
+ inherited_without_setup_and_teardown_methods(child) if respond_to?(:inherited_without_setup_and_teardown_methods, true)
93
+ child.instance_variable_set('@setup_methods', setup_methods.dup)
94
+ child.instance_variable_set('@teardown_methods', teardown_methods.dup)
95
+ end
95
96
 
96
- if respond_to?(:inherited, true)
97
- alias_method :inherited_without_setup_and_teardown_methods, :inherited
98
- end
99
- alias_method :inherited, :inherited_with_setup_and_teardown_methods
97
+ if respond_to?(:inherited, true)
98
+ alias_method :inherited_without_setup_and_teardown_methods, :inherited
99
+ end
100
+ alias_method :inherited, :inherited_with_setup_and_teardown_methods
100
101
 
101
- end
102
+ end
102
103
 
103
- def setup_mocha
104
- self.class.class_eval { setup_methods }.each { |symbol| send(symbol) }
105
- end
104
+ def setup_mocha
105
+ self.class.class_eval { setup_methods }.each { |symbol| send(symbol) }
106
+ end
106
107
 
107
- def teardown_mocha
108
- stored_exception = nil
109
- self.class.class_eval { teardown_methods }.reverse.each do |symbol|
110
- begin
111
- send(symbol)
112
- rescue Exception => e
113
- stored_exception ||= e
108
+ def teardown_mocha
109
+ stored_exception = nil
110
+ self.class.class_eval { teardown_methods }.reverse.each do |symbol|
111
+ begin
112
+ send(symbol)
113
+ rescue Exception => e
114
+ stored_exception ||= e
115
+ end
114
116
  end
117
+ raise stored_exception if stored_exception
115
118
  end
116
- raise stored_exception if stored_exception
119
+
117
120
  end
118
121
 
119
122
  end
120
123
 
121
124
  end
122
-
123
- end
125
+ end
@@ -4,5 +4,5 @@ require 'stubba/setup_and_teardown'
4
4
  require 'shared/backtracefilter'
5
5
 
6
6
  class Test::Unit::TestCase
7
- include SetupAndTeardown unless ancestors.include?(SetupAndTeardown)
7
+ include Stubba::SetupAndTeardown unless ancestors.include?(Stubba::SetupAndTeardown)
8
8
  end
@@ -3,43 +3,60 @@ require 'stubba/instance_method'
3
3
  require 'stubba/class_method'
4
4
  require 'stubba/any_instance_method'
5
5
 
6
+ # Methods added all Objects.
6
7
  class Object
7
8
 
8
- def mocha
9
+ def mocha # :nodoc:
9
10
  @mocha ||= Mocha::Mock.new
10
11
  end
11
12
 
12
- def reset_mocha
13
+ def reset_mocha # :nodoc:
13
14
  @mocha = nil
14
15
  end
15
16
 
16
- def stubba_method
17
+ def stubba_method # :nodoc:
17
18
  Stubba::InstanceMethod
18
19
  end
19
20
 
20
- def stubba_object
21
+ def stubba_object # :nodoc:
21
22
  self
22
23
  end
23
-
24
+
25
+ # :call-seq: expects(symbol) -> expectation
26
+ #
27
+ # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
28
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
29
+ # product = Product.new
30
+ # product.expects(:save).returns(true)
31
+ # assert_equal false, product.save
24
32
  def expects(symbol)
25
33
  method = stubba_method.new(stubba_object, symbol)
26
34
  $stubba.stub(method)
27
35
  mocha.expects(symbol, caller)
28
36
  end
29
37
 
38
+ # :call-seq: stubs(symbol) -> expectation
39
+ #
40
+ # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
41
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
42
+ # product = Product.new
43
+ # product.stubs(:save).returns(true)
44
+ # assert_equal false, product.save
30
45
  def stubs(symbol)
31
46
  method = stubba_method.new(stubba_object, symbol)
32
47
  $stubba.stub(method)
33
48
  mocha.stubs(symbol, caller)
34
49
  end
35
50
 
51
+ # Asserts that all expectations have been fulfilled.
52
+ # Called automatically at the end of a test for all objects with expectations.
36
53
  def verify
37
54
  mocha.verify
38
55
  end
39
56
 
40
57
  end
41
58
 
42
- class Module
59
+ class Module # :nodoc:
43
60
 
44
61
  def stubba_method
45
62
  Stubba::ClassMethod
@@ -49,11 +66,11 @@ end
49
66
 
50
67
  class Class
51
68
 
52
- def stubba_method
69
+ def stubba_method # :nodoc:
53
70
  Stubba::ClassMethod
54
71
  end
55
72
 
56
- class AnyInstance
73
+ class AnyInstance # :nodoc:
57
74
 
58
75
  def initialize(klass)
59
76
  @stubba_object = klass
@@ -69,6 +86,14 @@ class Class
69
86
 
70
87
  end
71
88
 
89
+ # :call-seq: any_instance -> mock object
90
+ #
91
+ # Returns a mock object which will detect calls to any instance of this class.
92
+ # Product.any_instance.stubs(:save).returns(false)
93
+ # product_1 = Product.new
94
+ # assert_equal false, product_1.save
95
+ # product_2 = Product.new
96
+ # assert_equal false, product_2.save
72
97
  def any_instance
73
98
  @any_instance ||= AnyInstance.new(self)
74
99
  end
@@ -1,25 +1,27 @@
1
1
  require 'stubba/central'
2
2
 
3
- module SetupAndTeardown
3
+ module Stubba
4
+ module SetupAndTeardown
4
5
 
5
- def self.included(base)
6
- base.add_setup_method(:setup_stubs)
7
- base.add_teardown_method(:teardown_stubs)
8
- end
6
+ def self.included(base)
7
+ base.add_setup_method(:setup_stubs)
8
+ base.add_teardown_method(:teardown_stubs)
9
+ end
9
10
 
10
- def setup_stubs
11
- $stubba = Stubba::Central.new
12
- end
11
+ def setup_stubs
12
+ $stubba = Stubba::Central.new
13
+ end
13
14
 
14
- def teardown_stubs
15
- if $stubba then
16
- begin
17
- $stubba.verify_all { add_assertion }
18
- ensure
19
- $stubba.unstub_all
20
- $stubba = nil
15
+ def teardown_stubs
16
+ if $stubba then
17
+ begin
18
+ $stubba.verify_all { add_assertion }
19
+ ensure
20
+ $stubba.unstub_all
21
+ $stubba = nil
22
+ end
21
23
  end
22
24
  end
23
- end
24
25
 
25
- end
26
+ end
27
+ end