mocha 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +2 -2
- data/RELEASE +6 -0
- data/Rakefile +1 -2
- data/lib/mocha.rb +35 -8
- data/lib/mocha/inspect.rb +48 -20
- data/lib/mocha/metaclass.rb +9 -3
- data/lib/mocha/mini_test_adapter.rb +50 -0
- data/lib/mocha/object.rb +118 -102
- data/lib/mocha/parameter_matchers/object.rb +9 -3
- data/test/acceptance/bug_21465_test.rb +1 -1
- data/test/acceptance/minitest_test.rb +130 -0
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +3 -1
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +3 -1
- data/test/acceptance/stubbing_non_public_class_method_test.rb +3 -1
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +3 -1
- data/test/method_definer.rb +16 -10
- metadata +5 -3
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Mocha is a library for mocking and stubbing using a syntax like that of JMock[http://www.jmock.org].
|
4
4
|
|
5
|
-
It can be used with many testing frameworks e.g. Test::Unit[http://www.ruby-doc.org/core/classes/Test/Unit.html], RSpec[http://rspec.info/], test/spec[http://chneukirchen.org/repos/testspec/README], expectations[http://expectations.rubyforge.org/], Dust[http://dust.rubyforge.org/] and even JtestR[http://jtestr.codehaus.org/].
|
5
|
+
It can be used with many testing frameworks e.g. Test::Unit[http://www.ruby-doc.org/core/classes/Test/Unit.html], RSpec[http://rspec.info/], test/spec[http://chneukirchen.org/repos/testspec/README], expectations[http://expectations.rubyforge.org/], Dust[http://dust.rubyforge.org/], MiniTest[http://rubyforge.org/projects/bfts] and even JtestR[http://jtestr.codehaus.org/].
|
6
6
|
|
7
7
|
Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking.
|
8
8
|
|
@@ -26,7 +26,7 @@ Or download Mocha from here - http://rubyforge.org/projects/mocha
|
|
26
26
|
* Traditional mocking - {Star Trek Example}[link:examples/mocha.html]
|
27
27
|
* Setting expectations on real classes - {Order Example}[link:examples/stubba.html]
|
28
28
|
* More examples on {Floehopper's Blog}[http://blog.floehopper.org]
|
29
|
-
* {Mailing List Archives}[http://
|
29
|
+
* {Mailing List Archives}[http://groups.google.com/group/mocha-developer]
|
30
30
|
|
31
31
|
== License
|
32
32
|
|
data/RELEASE
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
= 0.9.3 (8219bb2d2881c8529c93fc21e97a11d01203c759)
|
2
|
+
* Added support for MiniTest thanks to sprsquish.
|
3
|
+
* Fixed a possible bug with some of the non-default Configuration options relating to the argument to Object#respond_to?
|
4
|
+
* As per Jay Fields recommendations [1] and with further impetus from a talk at Ruby Manor, any methods added to core classes are now added by including a module. This means that Mocha is a better citizen of the Ruby world and it's behaviour is more easily extended. [1] http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html & http://blog.jayfields.com/2008/07/ruby-redefine-method-behavior.html
|
5
|
+
* Removed deprecated gem autorequire.
|
6
|
+
|
1
7
|
= 0.9.2 (r355)
|
2
8
|
* Improved documentation to address [#22530] 'Mock methods with multiple return values not possible?'
|
3
9
|
* respond_with parameter matcher was not available in tests.
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/testtask'
|
|
5
5
|
require 'rake/contrib/sshpublisher'
|
6
6
|
|
7
7
|
module Mocha
|
8
|
-
VERSION = "0.9.
|
8
|
+
VERSION = "0.9.3"
|
9
9
|
end
|
10
10
|
|
11
11
|
desc "Run all tests"
|
@@ -148,7 +148,6 @@ specification = Gem::Specification.new do |s|
|
|
148
148
|
s.extra_rdoc_files = ['README', 'COPYING']
|
149
149
|
s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
|
150
150
|
|
151
|
-
s.autorequire = 'mocha'
|
152
151
|
s.add_dependency('rake')
|
153
152
|
s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
|
154
153
|
end
|
data/lib/mocha.rb
CHANGED
@@ -1,20 +1,47 @@
|
|
1
1
|
require 'mocha_standalone'
|
2
|
-
require 'mocha/test_case_adapter'
|
3
2
|
require 'mocha/configuration'
|
4
3
|
|
4
|
+
if RUBY_VERSION < '1.9'
|
5
|
+
begin
|
6
|
+
require 'rubygems'
|
7
|
+
begin
|
8
|
+
gem 'minitest', '>=1.3'
|
9
|
+
require 'minitest/unit'
|
10
|
+
rescue Gem::LoadError
|
11
|
+
# Compatible version of MiniTest gem not available
|
12
|
+
end
|
13
|
+
rescue LoadError
|
14
|
+
# RubyGems not available
|
15
|
+
end
|
16
|
+
else
|
17
|
+
begin
|
18
|
+
require 'minitest/unit'
|
19
|
+
rescue LoadError
|
20
|
+
# MiniTest not available
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if defined?(MiniTest)
|
25
|
+
require 'mocha/mini_test_adapter'
|
26
|
+
|
27
|
+
module MiniTest
|
28
|
+
class Unit
|
29
|
+
class TestCase
|
30
|
+
include Mocha::Standalone
|
31
|
+
include Mocha::MiniTestCaseAdapter
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'mocha/test_case_adapter'
|
5
38
|
require 'test/unit/testcase'
|
6
39
|
|
7
40
|
module Test
|
8
|
-
|
9
41
|
module Unit
|
10
|
-
|
11
42
|
class TestCase
|
12
|
-
|
13
43
|
include Mocha::Standalone
|
14
44
|
include Mocha::TestCaseAdapter
|
15
|
-
|
16
45
|
end
|
17
|
-
|
18
46
|
end
|
19
|
-
|
20
|
-
end
|
47
|
+
end
|
data/lib/mocha/inspect.rb
CHANGED
@@ -1,39 +1,67 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module Mocha
|
4
|
+
|
5
|
+
module ObjectMethods
|
6
|
+
def mocha_inspect
|
7
|
+
address = self.__id__ * 2
|
8
|
+
address += 0x100000000 if address < 0
|
9
|
+
inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module StringMethods
|
14
|
+
def mocha_inspect
|
15
|
+
inspect.gsub(/\"/, "'")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module ArrayMethods
|
20
|
+
def mocha_inspect
|
21
|
+
"[#{collect { |member| member.mocha_inspect }.join(', ')}]"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module HashMethods
|
26
|
+
def mocha_inspect
|
27
|
+
"{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
|
28
|
+
end
|
8
29
|
end
|
30
|
+
|
31
|
+
module TimeMethods
|
32
|
+
def mocha_inspect
|
33
|
+
"#{inspect} (#{to_f} secs)"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module DateMethods
|
38
|
+
def mocha_inspect
|
39
|
+
to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class Object
|
46
|
+
include Mocha::ObjectMethods
|
9
47
|
end
|
10
48
|
|
11
49
|
class String
|
12
|
-
|
13
|
-
inspect.gsub(/\"/, "'")
|
14
|
-
end
|
50
|
+
include Mocha::StringMethods
|
15
51
|
end
|
16
52
|
|
17
53
|
class Array
|
18
|
-
|
19
|
-
"[#{collect { |member| member.mocha_inspect }.join(', ')}]"
|
20
|
-
end
|
54
|
+
include Mocha::ArrayMethods
|
21
55
|
end
|
22
56
|
|
23
57
|
class Hash
|
24
|
-
|
25
|
-
"{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
|
26
|
-
end
|
58
|
+
include Mocha::HashMethods
|
27
59
|
end
|
28
60
|
|
29
61
|
class Time
|
30
|
-
|
31
|
-
"#{inspect} (#{to_f} secs)"
|
32
|
-
end
|
62
|
+
include Mocha::TimeMethods
|
33
63
|
end
|
34
64
|
|
35
65
|
class Date
|
36
|
-
|
37
|
-
to_s
|
38
|
-
end
|
66
|
+
include Mocha::DateMethods
|
39
67
|
end
|
data/lib/mocha/metaclass.rb
CHANGED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Mocha
|
2
|
+
|
3
|
+
module MiniTestCaseAdapter
|
4
|
+
|
5
|
+
class AssertionCounter
|
6
|
+
def initialize(test_case)
|
7
|
+
@test_case = test_case
|
8
|
+
end
|
9
|
+
|
10
|
+
def increment
|
11
|
+
@test_case._assertions += 1
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.included(base)
|
16
|
+
base.class_eval do
|
17
|
+
|
18
|
+
alias_method :run_before_mocha_mini_test_adapter, :run
|
19
|
+
|
20
|
+
def run runner
|
21
|
+
assertion_counter = AssertionCounter.new(self)
|
22
|
+
result = '.'
|
23
|
+
begin
|
24
|
+
begin
|
25
|
+
@passed = nil
|
26
|
+
self.setup
|
27
|
+
self.__send__ self.name
|
28
|
+
mocha_verify(assertion_counter)
|
29
|
+
@passed = true
|
30
|
+
rescue Exception => e
|
31
|
+
@passed = false
|
32
|
+
result = runner.puke(self.class, self.name, e)
|
33
|
+
ensure
|
34
|
+
begin
|
35
|
+
self.teardown
|
36
|
+
rescue Exception => e
|
37
|
+
result = runner.puke(self.class, self.name, e)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
ensure
|
41
|
+
mocha_teardown
|
42
|
+
end
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
data/lib/mocha/object.rb
CHANGED
@@ -4,131 +4,147 @@ require 'mocha/class_method'
|
|
4
4
|
require 'mocha/module_method'
|
5
5
|
require 'mocha/any_instance_method'
|
6
6
|
|
7
|
-
|
8
|
-
#
|
9
|
-
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
|
10
|
-
class Object
|
7
|
+
module Mocha
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
# Methods added all objects to allow mocking and stubbing on real objects.
|
10
|
+
#
|
11
|
+
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
|
12
|
+
module ObjectMethods
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def mocha # :nodoc:
|
15
|
+
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
|
16
|
+
end
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def reset_mocha # :nodoc:
|
19
|
+
@mocha = nil
|
20
|
+
end
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
def stubba_method # :nodoc:
|
23
|
+
Mocha::InstanceMethod
|
24
|
+
end
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
32
|
-
# product = Product.new
|
33
|
-
# product.expects(:save).returns(true)
|
34
|
-
# assert_equal false, product.save
|
35
|
-
#
|
36
|
-
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
|
37
|
-
#
|
38
|
-
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
39
|
-
def expects(symbol)
|
40
|
-
mockery = Mocha::Mockery.instance
|
41
|
-
mockery.on_stubbing(self, symbol)
|
42
|
-
method = stubba_method.new(stubba_object, symbol)
|
43
|
-
mockery.stubba.stub(method)
|
44
|
-
mocha.expects(symbol, caller)
|
45
|
-
end
|
26
|
+
def stubba_object # :nodoc:
|
27
|
+
self
|
28
|
+
end
|
46
29
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
30
|
+
# :call-seq: expects(symbol) -> expectation
|
31
|
+
#
|
32
|
+
# Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
|
33
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
34
|
+
# product = Product.new
|
35
|
+
# product.expects(:save).returns(true)
|
36
|
+
# assert_equal false, product.save
|
37
|
+
#
|
38
|
+
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
|
39
|
+
#
|
40
|
+
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
41
|
+
def expects(symbol)
|
42
|
+
mockery = Mocha::Mockery.instance
|
43
|
+
mockery.on_stubbing(self, symbol)
|
44
|
+
method = stubba_method.new(stubba_object, symbol)
|
45
|
+
mockery.stubba.stub(method)
|
46
|
+
mocha.expects(symbol, caller)
|
47
|
+
end
|
65
48
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
49
|
+
# :call-seq: stubs(symbol) -> expectation
|
50
|
+
#
|
51
|
+
# Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
|
52
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
53
|
+
# product = Product.new
|
54
|
+
# product.stubs(:save).returns(true)
|
55
|
+
# assert_equal false, product.save
|
56
|
+
#
|
57
|
+
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
|
58
|
+
#
|
59
|
+
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
60
|
+
def stubs(symbol)
|
61
|
+
mockery = Mocha::Mockery.instance
|
62
|
+
mockery.on_stubbing(self, symbol)
|
63
|
+
method = stubba_method.new(stubba_object, symbol)
|
64
|
+
mockery.stubba.stub(method)
|
65
|
+
mocha.stubs(symbol, caller)
|
70
66
|
end
|
71
|
-
return true if protected_methods(include_superclass_methods = true).include?(method)
|
72
|
-
return true if private_methods(include_superclass_methods = true).include?(method)
|
73
|
-
return false
|
74
|
-
end
|
75
67
|
|
76
|
-
|
77
|
-
|
78
|
-
|
68
|
+
def method_exists?(method, include_public_methods = true) # :nodoc:
|
69
|
+
if include_public_methods
|
70
|
+
return true if public_methods(include_superclass_methods = true).include?(method)
|
71
|
+
return true if respond_to?(method.to_sym)
|
72
|
+
end
|
73
|
+
return true if protected_methods(include_superclass_methods = true).include?(method)
|
74
|
+
return true if private_methods(include_superclass_methods = true).include?(method)
|
75
|
+
return false
|
76
|
+
end
|
79
77
|
|
80
|
-
def stubba_method
|
81
|
-
Mocha::ModuleMethod
|
82
78
|
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
class Class
|
87
79
|
|
88
|
-
|
89
|
-
Mocha::ClassMethod
|
90
|
-
end
|
91
|
-
|
92
|
-
class AnyInstance # :nodoc:
|
80
|
+
module ModuleMethods # :nodoc:
|
93
81
|
|
94
|
-
def
|
95
|
-
|
82
|
+
def stubba_method
|
83
|
+
Mocha::ModuleMethod
|
96
84
|
end
|
97
85
|
|
98
|
-
|
99
|
-
|
86
|
+
end
|
87
|
+
|
88
|
+
# Methods added all classes to allow mocking and stubbing on real objects.
|
89
|
+
module ClassMethods
|
90
|
+
|
91
|
+
def stubba_method # :nodoc:
|
92
|
+
Mocha::ClassMethod
|
100
93
|
end
|
101
94
|
|
102
|
-
|
103
|
-
Mocha::AnyInstanceMethod
|
104
|
-
end
|
95
|
+
class AnyInstance # :nodoc:
|
105
96
|
|
106
|
-
|
107
|
-
|
108
|
-
|
97
|
+
def initialize(klass)
|
98
|
+
@stubba_object = klass
|
99
|
+
end
|
109
100
|
|
110
|
-
|
111
|
-
|
112
|
-
|
101
|
+
def mocha
|
102
|
+
@mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
|
103
|
+
end
|
104
|
+
|
105
|
+
def stubba_method
|
106
|
+
Mocha::AnyInstanceMethod
|
113
107
|
end
|
114
|
-
return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
|
115
|
-
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
|
116
|
-
return false
|
117
|
-
end
|
118
108
|
|
119
|
-
|
109
|
+
def stubba_object
|
110
|
+
@stubba_object
|
111
|
+
end
|
112
|
+
|
113
|
+
def method_exists?(method, include_public_methods = true)
|
114
|
+
if include_public_methods
|
115
|
+
return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
|
116
|
+
end
|
117
|
+
return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
|
118
|
+
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
|
119
|
+
return false
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
# :call-seq: any_instance -> mock object
|
125
|
+
#
|
126
|
+
# Returns a mock object which will detect calls to any instance of this class.
|
127
|
+
# Product.any_instance.stubs(:save).returns(false)
|
128
|
+
# product_1 = Product.new
|
129
|
+
# assert_equal false, product_1.save
|
130
|
+
# product_2 = Product.new
|
131
|
+
# assert_equal false, product_2.save
|
132
|
+
def any_instance
|
133
|
+
@any_instance ||= AnyInstance.new(self)
|
134
|
+
end
|
120
135
|
|
121
|
-
# :call-seq: any_instance -> mock object
|
122
|
-
#
|
123
|
-
# Returns a mock object which will detect calls to any instance of this class.
|
124
|
-
# Product.any_instance.stubs(:save).returns(false)
|
125
|
-
# product_1 = Product.new
|
126
|
-
# assert_equal false, product_1.save
|
127
|
-
# product_2 = Product.new
|
128
|
-
# assert_equal false, product_2.save
|
129
|
-
def any_instance
|
130
|
-
@any_instance ||= AnyInstance.new(self)
|
131
136
|
end
|
132
137
|
|
133
138
|
end
|
134
139
|
|
140
|
+
class Object # :nodoc:
|
141
|
+
include Mocha::ObjectMethods
|
142
|
+
end
|
143
|
+
|
144
|
+
class Module # :nodoc:
|
145
|
+
include Mocha::ModuleMethods
|
146
|
+
end
|
147
|
+
|
148
|
+
class Class # :nodoc:
|
149
|
+
include Mocha::ClassMethods
|
150
|
+
end
|
@@ -1,9 +1,15 @@
|
|
1
1
|
require 'mocha/parameter_matchers/equals'
|
2
2
|
|
3
|
-
|
3
|
+
module Mocha
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
module ObjectMethods
|
6
|
+
def to_matcher # :nodoc:
|
7
|
+
Mocha::ParameterMatchers::Equals.new(self)
|
8
|
+
end
|
7
9
|
end
|
8
10
|
|
9
11
|
end
|
12
|
+
|
13
|
+
class Object
|
14
|
+
include Mocha::ObjectMethods
|
15
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
if defined?(MiniTest)
|
5
|
+
|
6
|
+
class MiniTestSampleTest < MiniTest::Unit::TestCase
|
7
|
+
|
8
|
+
def test_mocha_with_fulfilled_expectation
|
9
|
+
mockee = mock()
|
10
|
+
mockee.expects(:blah)
|
11
|
+
mockee.blah
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_mocha_with_unfulfilled_expectation
|
15
|
+
mockee = mock()
|
16
|
+
mockee.expects(:blah)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_mocha_with_unexpected_invocation
|
20
|
+
mockee = mock()
|
21
|
+
mockee.blah
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_stubba_with_fulfilled_expectation
|
25
|
+
stubbee = Class.new { define_method(:blah) {} }.new
|
26
|
+
stubbee.expects(:blah)
|
27
|
+
stubbee.blah
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_stubba_with_unfulfilled_expectation
|
31
|
+
stubbee = Class.new { define_method(:blah) {} }.new
|
32
|
+
stubbee.expects(:blah)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_mocha_with_matching_parameter
|
36
|
+
mockee = mock()
|
37
|
+
mockee.expects(:blah).with(has_key(:wibble))
|
38
|
+
mockee.blah(:wibble => 1)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_mocha_with_non_matching_parameter
|
42
|
+
mockee = mock()
|
43
|
+
mockee.expects(:blah).with(has_key(:wibble))
|
44
|
+
mockee.blah(:wobble => 2)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class MiniTestAdapterTest < Test::Unit::TestCase
|
50
|
+
|
51
|
+
def setup
|
52
|
+
@output = StringIO.new
|
53
|
+
MiniTest::Unit.output = @output
|
54
|
+
@runner = MiniTest::Unit.new
|
55
|
+
end
|
56
|
+
|
57
|
+
attr_reader :runner
|
58
|
+
|
59
|
+
def test_should_pass_mocha_test
|
60
|
+
runner.run(%w(-n test_mocha_with_fulfilled_expectation))
|
61
|
+
|
62
|
+
assert_equal 0, runner.errors
|
63
|
+
assert_equal 1, runner.assertion_count
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_should_fail_mocha_test_due_to_unfulfilled_expectation
|
67
|
+
runner.run(%w(-n test_mocha_with_unfulfilled_expectation))
|
68
|
+
|
69
|
+
assert_equal 1, runner.errors
|
70
|
+
assert_equal 1, runner.assertion_count
|
71
|
+
assert_not_all_expectation_were_satisfied
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_fail_mocha_test_due_to_unexpected_invocation
|
75
|
+
runner.run(%w(-n test_mocha_with_unexpected_invocation))
|
76
|
+
|
77
|
+
assert_equal 1, runner.errors
|
78
|
+
assert_equal 0, runner.assertion_count
|
79
|
+
assert_unexpected_invocation
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_pass_stubba_test
|
83
|
+
runner.run(%w(-n test_stubba_with_fulfilled_expectation))
|
84
|
+
|
85
|
+
assert_equal 0, runner.errors
|
86
|
+
assert_equal 1, runner.assertion_count
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_should_fail_stubba_test_due_to_unfulfilled_expectation
|
90
|
+
runner.run(%w(-n test_stubba_with_unfulfilled_expectation))
|
91
|
+
|
92
|
+
assert_equal 1, runner.errors
|
93
|
+
assert_equal 1, runner.assertion_count
|
94
|
+
assert_match Regexp.new('not all expectations were satisfied'), output
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_should_pass_mocha_test_with_matching_parameter
|
98
|
+
runner.run(%w(-n test_mocha_with_matching_parameter))
|
99
|
+
|
100
|
+
assert_equal 0, runner.errors
|
101
|
+
assert_equal 1, runner.assertion_count
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_should_fail_mocha_test_with_non_matching_parameter
|
105
|
+
runner.run(%w(-n test_mocha_with_non_matching_parameter))
|
106
|
+
|
107
|
+
assert_equal 1, runner.errors
|
108
|
+
assert_unexpected_invocation
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def output
|
114
|
+
@output.rewind
|
115
|
+
@output.read
|
116
|
+
end
|
117
|
+
|
118
|
+
def assert_unexpected_invocation
|
119
|
+
assert_match Regexp.new('unexpected invocation'), output, "MiniTest output:\n#{output}"
|
120
|
+
end
|
121
|
+
|
122
|
+
def assert_not_all_expectation_were_satisfied
|
123
|
+
assert_match Regexp.new('not all expectations were satisfied'), output, "MiniTest output:\n#{output}"
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
else
|
129
|
+
warn "MiniTest is not available, so MiniTestAdapterTest has not been run."
|
130
|
+
end
|
@@ -70,7 +70,9 @@ class StubbingNonExistentClassMethodTest < Test::Unit::TestCase
|
|
70
70
|
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
71
71
|
klass = Class.new do
|
72
72
|
class << self
|
73
|
-
def respond_to?(method, include_private = false)
|
73
|
+
def respond_to?(method, include_private = false)
|
74
|
+
(method == :method_to_which_class_responds)
|
75
|
+
end
|
74
76
|
end
|
75
77
|
end
|
76
78
|
test_result = run_test do
|
@@ -68,7 +68,9 @@ class StubbingNonExistentInstanceMethodTest < Test::Unit::TestCase
|
|
68
68
|
def test_should_allow_stubbing_method_to_which_instance_responds
|
69
69
|
Mocha::Configuration.prevent(:stubbing_non_existent_method)
|
70
70
|
klass = Class.new do
|
71
|
-
def respond_to?(method, include_private = false)
|
71
|
+
def respond_to?(method, include_private = false)
|
72
|
+
(method == :method_to_which_instance_responds)
|
73
|
+
end
|
72
74
|
end
|
73
75
|
instance = klass.new
|
74
76
|
test_result = run_test do
|
@@ -149,7 +149,9 @@ class StubbingNonPublicClassMethodTest < Test::Unit::TestCase
|
|
149
149
|
Mocha::Configuration.prevent(:stubbing_non_public_method)
|
150
150
|
klass = Class.new do
|
151
151
|
class << self
|
152
|
-
def respond_to?(method, include_private_methods = false)
|
152
|
+
def respond_to?(method, include_private_methods = false)
|
153
|
+
(method == :method_to_which_class_responds)
|
154
|
+
end
|
153
155
|
end
|
154
156
|
end
|
155
157
|
test_result = run_test do
|
@@ -130,7 +130,9 @@ class StubbingNonPublicInstanceMethodTest < Test::Unit::TestCase
|
|
130
130
|
def test_should_allow_stubbing_method_to_which_instance_responds
|
131
131
|
Mocha::Configuration.prevent(:stubbing_non_public_method)
|
132
132
|
instance = Class.new do
|
133
|
-
def respond_to?(method, include_private_methods = false)
|
133
|
+
def respond_to?(method, include_private_methods = false)
|
134
|
+
(method == :method_to_which_instance_responds)
|
135
|
+
end
|
134
136
|
end.new
|
135
137
|
test_result = run_test do
|
136
138
|
instance.stubs(:method_to_which_instance_responds)
|
data/test/method_definer.rb
CHANGED
@@ -1,18 +1,24 @@
|
|
1
1
|
require 'mocha/metaclass'
|
2
2
|
|
3
|
-
|
3
|
+
module Mocha
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
module ObjectMethods
|
6
|
+
def define_instance_method(method_symbol, &block)
|
7
|
+
__metaclass__.send(:define_method, method_symbol, block)
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def replace_instance_method(method_symbol, &block)
|
11
|
+
raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless self.respond_to?(method_symbol)
|
12
|
+
define_instance_method(method_symbol, &block)
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
def define_instance_accessor(*symbols)
|
16
|
+
symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
|
17
|
+
end
|
16
18
|
end
|
19
|
+
|
20
|
+
end
|
17
21
|
|
22
|
+
class Object
|
23
|
+
include Mocha::ObjectMethods
|
18
24
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mocha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Mead
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-11-
|
12
|
+
date: 2008-11-27 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- lib/mocha/logger.rb
|
52
52
|
- lib/mocha/metaclass.rb
|
53
53
|
- lib/mocha/method_matcher.rb
|
54
|
+
- lib/mocha/mini_test_adapter.rb
|
54
55
|
- lib/mocha/mock.rb
|
55
56
|
- lib/mocha/mockery.rb
|
56
57
|
- lib/mocha/module_method.rb
|
@@ -100,6 +101,7 @@ files:
|
|
100
101
|
- test/acceptance/bug_21563_test.rb
|
101
102
|
- test/acceptance/expected_invocation_count_test.rb
|
102
103
|
- test/acceptance/failure_messages_test.rb
|
104
|
+
- test/acceptance/minitest_test.rb
|
103
105
|
- test/acceptance/mocha_example_test.rb
|
104
106
|
- test/acceptance/mocha_test_result_test.rb
|
105
107
|
- test/acceptance/mock_test.rb
|