mocha 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -6,7 +6,7 @@ It can be used with many testing frameworks e.g. Test::Unit[http://www.ruby-doc.
6
6
 
7
7
  Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking.
8
8
 
9
- Mocha was harvested from projects at Reevoo[http://www.reevoo.com] by me (James[http://blog.floehopper.org]) and my colleagues Ben[http://www.techbelly.com/], Chris[http://blog.seagul.co.uk] and Paul[http://po-ru.com].
9
+ Mocha was harvested from projects at Reevoo[http://www.reevoo.com] by me (James[http://blog.floehopper.org]) and my (then) colleagues Ben[http://www.techbelly.com/], Chris[http://chrisroos.co.uk] and Paul[http://po-ru.com].
10
10
 
11
11
  == Download and Installation
12
12
 
@@ -16,9 +16,11 @@ Install the gem with the following command...
16
16
 
17
17
  Or install the Rails[http://www.rubyonrails.org] plugin...
18
18
 
19
- $ script/plugin install svn://rubyforge.org/var/svn/mocha/trunk
19
+ $ script/plugin install git://github.com/floehopper/mocha.git
20
20
 
21
- Or download Mocha from here - http://rubyforge.org/projects/mocha
21
+ Or download Mocha...
22
+
23
+ http://rubyforge.org/frs/?group_id=1917
22
24
 
23
25
  == Examples
24
26
 
data/RELEASE CHANGED
@@ -1,3 +1,9 @@
1
+ = 0.9.4 (8a59c6ff0f99f34b02bd99f19536a7893be2b340)
2
+ * Added mocha.gemspec file generated with Chad Woolley's new rake task, so that a floehopper-mocha gem will get built on GitHub.
3
+ * Add rake task to update mocha.gemspec with unique version, which will cause gem to be auto-built on github
4
+ * As Tobias Crawley correctly pointed out in feature request #23055 "stubs(with_hash) not working with existing object" [1], following the principle of least surprise, it should be possible to call ObjectMethods#expects & ObjectMethods#stubs with a Hash of method_names vs return_values like you can with Mock#expects & Mock#stubs. I've also updated & improved the docs to reflect the changes. [1] http://rubyforge.org/tracker/index.php?func=detail&aid=23055&group_id=1917&atid=7480
5
+ * Removed deprecated gem autorequire.
6
+
1
7
  = 0.9.3 (8219bb2d2881c8529c93fc21e97a11d01203c759)
2
8
  * Added support for MiniTest thanks to sprsquish.
3
9
  * Fixed a possible bug with some of the non-default Configuration options relating to the argument to Object#respond_to?
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.3"
8
+ VERSION = "0.9.4"
9
9
  end
10
10
 
11
11
  desc "Run all tests"
@@ -131,32 +131,44 @@ end
131
131
 
132
132
  Gem.manage_gems if Gem::RubyGemsVersion < '1.2.0'
133
133
 
134
- specification = Gem::Specification.new do |s|
135
- s.name = "mocha"
136
- s.summary = "Mocking and stubbing library"
137
- s.version = Mocha::VERSION
138
- s.platform = Gem::Platform::RUBY
139
- s.author = 'James Mead'
140
- s.description = <<-EOF
141
- Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
142
- EOF
143
- s.email = 'mocha-developer@rubyforge.org'
144
- s.homepage = 'http://mocha.rubyforge.org'
145
- s.rubyforge_project = 'mocha'
146
-
147
- s.has_rdoc = true
148
- s.extra_rdoc_files = ['README', 'COPYING']
149
- s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
150
-
151
- s.add_dependency('rake')
152
- s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
134
+ def build_specification(version = Mocha::VERSION)
135
+ Gem::Specification.new do |s|
136
+ s.name = "mocha"
137
+ s.summary = "Mocking and stubbing library"
138
+ s.version = version
139
+ s.platform = Gem::Platform::RUBY
140
+ s.author = 'James Mead'
141
+ s.description = <<-EOF
142
+ Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
143
+ EOF
144
+ s.email = 'mocha-developer@googlegroups.com'
145
+ s.homepage = 'http://mocha.rubyforge.org'
146
+ s.rubyforge_project = 'mocha'
147
+
148
+ s.has_rdoc = true
149
+ s.extra_rdoc_files = ['README', 'COPYING']
150
+ s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
151
+
152
+ s.add_dependency('rake')
153
+ s.files = FileList['{lib,test,examples}/**/*.rb', '[A-Z]*'].exclude('TODO').to_a
154
+ end
153
155
  end
154
156
 
157
+ specification = build_specification
158
+
155
159
  Rake::GemPackageTask.new(specification) do |package|
156
160
  package.need_zip = true
157
161
  package.need_tar = true
158
162
  end
159
163
 
164
+ desc 'Generate updated gemspec with unique version, which will cause gem to be auto-built on github.'
165
+ task :update_gemspec do
166
+ File.open('mocha.gemspec', 'w') do |output|
167
+ output << build_specification(Mocha::VERSION + '.' + Time.now.strftime('%Y%m%d%H%M%S')).to_ruby
168
+ end
169
+ end
170
+
171
+
160
172
  task 'verify_user' do
161
173
  raise "RUBYFORGE_USER environment variable not set!" unless ENV['RUBYFORGE_USER']
162
174
  end
@@ -0,0 +1,21 @@
1
+ module Mocha
2
+
3
+ class ArgumentIterator
4
+
5
+ def initialize(argument)
6
+ @argument = argument
7
+ end
8
+
9
+ def each(&block)
10
+ if @argument.is_a?(Hash) then
11
+ @argument.each do |method_name, return_value|
12
+ block.call(method_name, return_value)
13
+ end
14
+ else
15
+ block.call(@argument)
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -6,6 +6,7 @@ require 'mocha/mockery'
6
6
  require 'mocha/method_matcher'
7
7
  require 'mocha/parameters_matcher'
8
8
  require 'mocha/unexpected_invocation'
9
+ require 'mocha/argument_iterator'
9
10
 
10
11
  module Mocha # :nodoc:
11
12
 
@@ -15,9 +16,9 @@ module Mocha # :nodoc:
15
16
  class Mock
16
17
 
17
18
  # :call-seq: expects(method_name) -> expectation
18
- # expects(method_names) -> last expectation
19
+ # expects(method_names_vs_return_values) -> last expectation
19
20
  #
20
- # Adds an expectation that a method identified by +method_name+ symbol must be called exactly once with any parameters.
21
+ # Adds an expectation that a method identified by +method_name+ Symbol/String must be called exactly once with any parameters.
21
22
  # Returns the new expectation which can be further modified by methods on Expectation.
22
23
  # object = mock()
23
24
  # object.expects(:method1)
@@ -27,7 +28,7 @@ module Mocha # :nodoc:
27
28
  # object = mock()
28
29
  # object.expects(:method1)
29
30
  # # error raised, because method1 not called exactly once
30
- # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
31
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
31
32
  # object = mock()
32
33
  # object.expects(:method1 => :result1, :method2 => :result2)
33
34
  #
@@ -39,28 +40,27 @@ module Mocha # :nodoc:
39
40
  #
40
41
  # Aliased by <tt>\_\_expects\_\_</tt>
41
42
  def expects(method_name_or_hash, backtrace = nil)
42
- if method_name_or_hash.is_a?(Hash) then
43
- method_name_or_hash.each do |method_name, return_value|
44
- ensure_method_not_already_defined(method_name)
45
- @expectations.add(Expectation.new(self, method_name, backtrace).returns(return_value))
46
- end
47
- else
48
- ensure_method_not_already_defined(method_name_or_hash)
49
- @expectations.add(Expectation.new(self, method_name_or_hash, backtrace))
50
- end
43
+ iterator = ArgumentIterator.new(method_name_or_hash)
44
+ iterator.each { |*args|
45
+ method_name = args.shift
46
+ ensure_method_not_already_defined(method_name)
47
+ expectation = Expectation.new(self, method_name, backtrace)
48
+ expectation.returns(args.shift) if args.length > 0
49
+ @expectations.add(expectation)
50
+ }
51
51
  end
52
52
 
53
53
  # :call-seq: stubs(method_name) -> expectation
54
- # stubs(method_names) -> last expectation
54
+ # stubs(method_names_vs_return_values) -> last expectation
55
55
  #
56
- # Adds an expectation that a method identified by +method_name+ symbol may be called any number of times with any parameters.
56
+ # Adds an expectation that a method identified by +method_name+ Symbol/String may be called any number of times with any parameters.
57
57
  # Returns the new expectation which can be further modified by methods on Expectation.
58
58
  # object = mock()
59
59
  # object.stubs(:method1)
60
60
  # object.method1
61
61
  # object.method1
62
62
  # # no error raised
63
- # If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
63
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
64
64
  # object = mock()
65
65
  # object.stubs(:method1 => :result1, :method2 => :result2)
66
66
  #
@@ -72,15 +72,15 @@ module Mocha # :nodoc:
72
72
  #
73
73
  # Aliased by <tt>\_\_stubs\_\_</tt>
74
74
  def stubs(method_name_or_hash, backtrace = nil)
75
- if method_name_or_hash.is_a?(Hash) then
76
- method_name_or_hash.each do |method_name, return_value|
77
- ensure_method_not_already_defined(method_name)
78
- @expectations.add(Expectation.new(self, method_name, backtrace).at_least(0).returns(return_value))
79
- end
80
- else
81
- ensure_method_not_already_defined(method_name_or_hash)
82
- @expectations.add(Expectation.new(self, method_name_or_hash, backtrace).at_least(0))
83
- end
75
+ iterator = ArgumentIterator.new(method_name_or_hash)
76
+ iterator.each { |*args|
77
+ method_name = args.shift
78
+ ensure_method_not_already_defined(method_name)
79
+ expectation = Expectation.new(self, method_name, backtrace)
80
+ expectation.at_least(0)
81
+ expectation.returns(args.shift) if args.length > 0
82
+ @expectations.add(expectation)
83
+ }
84
84
  end
85
85
 
86
86
  # :call-seq: responds_like(responder) -> mock
@@ -3,6 +3,7 @@ require 'mocha/instance_method'
3
3
  require 'mocha/class_method'
4
4
  require 'mocha/module_method'
5
5
  require 'mocha/any_instance_method'
6
+ require 'mocha/argument_iterator'
6
7
 
7
8
  module Mocha
8
9
 
@@ -27,42 +28,78 @@ module Mocha
27
28
  self
28
29
  end
29
30
 
30
- # :call-seq: expects(symbol) -> expectation
31
+ # :call-seq: expects(method_name) -> expectation
32
+ # expects(method_names_vs_return_values) -> last expectation
31
33
  #
32
- # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
34
+ # Adds an expectation that a method identified by +method_name+ Symbol must be called exactly once with any parameters.
33
35
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
34
36
  # product = Product.new
35
37
  # product.expects(:save).returns(true)
36
- # assert_equal false, product.save
38
+ # assert_equal true, product.save
37
39
  #
38
40
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
39
41
  #
40
42
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
41
- def expects(symbol)
43
+ #
44
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
45
+ # product = Product.new
46
+ # product.expects(:valid? => true, :save => true)
47
+ #
48
+ # # exactly equivalent to
49
+ #
50
+ # product = Product.new
51
+ # product.expects(:valid?).returns(true)
52
+ # product.expects(:save).returns(true)
53
+ def expects(method_name_or_hash)
54
+ expectation = nil
42
55
  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)
56
+ iterator = ArgumentIterator.new(method_name_or_hash)
57
+ iterator.each { |*args|
58
+ method_name = args.shift
59
+ mockery.on_stubbing(self, method_name)
60
+ method = stubba_method.new(stubba_object, method_name)
61
+ mockery.stubba.stub(method)
62
+ expectation = mocha.expects(method_name, caller)
63
+ expectation.returns(args.shift) if args.length > 0
64
+ }
65
+ expectation
47
66
  end
48
67
 
49
- # :call-seq: stubs(symbol) -> expectation
68
+ # :call-seq: stubs(method_name) -> expectation
69
+ # stubs(method_names_vs_return_values) -> last expectation
50
70
  #
51
- # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
71
+ # Adds an expectation that a method identified by +method_name+ Symbol may be called any number of times with any parameters.
52
72
  # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
53
73
  # product = Product.new
54
74
  # product.stubs(:save).returns(true)
55
- # assert_equal false, product.save
75
+ # assert_equal true, product.save
56
76
  #
57
77
  # The original implementation of <tt>Product#save</tt> is replaced temporarily.
58
78
  #
59
79
  # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
60
- def stubs(symbol)
80
+ #
81
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
82
+ # product = Product.new
83
+ # product.stubs(:valid? => true, :save => true)
84
+ #
85
+ # # exactly equivalent to
86
+ #
87
+ # product = Product.new
88
+ # product.stubs(:valid?).returns(true)
89
+ # product.stubs(:save).returns(true)
90
+ def stubs(method_name_or_hash)
91
+ expectation = nil
61
92
  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)
93
+ iterator = ArgumentIterator.new(method_name_or_hash)
94
+ iterator.each { |*args|
95
+ method_name = args.shift
96
+ mockery.on_stubbing(self, method_name)
97
+ method = stubba_method.new(stubba_object, method_name)
98
+ mockery.stubba.stub(method)
99
+ expectation = mocha.stubs(method_name, caller)
100
+ expectation.returns(args.shift) if args.length > 0
101
+ }
102
+ expectation
66
103
  end
67
104
 
68
105
  def method_exists?(method, include_public_methods = true) # :nodoc:
@@ -162,4 +162,42 @@ class StubInstanceMethodTest < Test::Unit::TestCase
162
162
  assert_passed(test_result)
163
163
  end
164
164
 
165
+ def test_should_be_able_to_specify_expectations_on_multiple_methods_in_a_single_call_to_expects
166
+ instance = Class.new do
167
+ def my_instance_method_1
168
+ :original_return_value_1
169
+ end
170
+ def my_instance_method_2
171
+ :original_return_value_2
172
+ end
173
+ end.new
174
+ run_test do
175
+ instance.expects(
176
+ :my_instance_method_1 => :new_return_value_1,
177
+ :my_instance_method_2 => :new_return_value_2
178
+ )
179
+ assert_equal :new_return_value_1, instance.my_instance_method_1
180
+ assert_equal :new_return_value_2, instance.my_instance_method_2
181
+ end
182
+ end
183
+
184
+ def test_should_be_able_to_specify_expectations_on_multiple_methods_in_a_single_call_to_stubs
185
+ instance = Class.new do
186
+ def my_instance_method_1
187
+ :original_return_value_1
188
+ end
189
+ def my_instance_method_2
190
+ :original_return_value_2
191
+ end
192
+ end.new
193
+ run_test do
194
+ instance.stubs(
195
+ :my_instance_method_1 => :new_return_value_1,
196
+ :my_instance_method_2 => :new_return_value_2
197
+ )
198
+ assert_equal :new_return_value_1, instance.my_instance_method_1
199
+ assert_equal :new_return_value_2, instance.my_instance_method_2
200
+ end
201
+ end
202
+
165
203
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-27 00:00:00 +00:00
12
+ date: 2008-12-30 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -23,7 +23,7 @@ dependencies:
23
23
  version: "0"
24
24
  version:
25
25
  description: Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
26
- email: mocha-developer@rubyforge.org
26
+ email: mocha-developer@googlegroups.com
27
27
  executables: []
28
28
 
29
29
  extensions: []
@@ -33,6 +33,7 @@ extra_rdoc_files:
33
33
  - COPYING
34
34
  files:
35
35
  - lib/mocha/any_instance_method.rb
36
+ - lib/mocha/argument_iterator.rb
36
37
  - lib/mocha/backtrace_filter.rb
37
38
  - lib/mocha/cardinality.rb
38
39
  - lib/mocha/central.rb
@@ -132,7 +133,6 @@ files:
132
133
  - test/acceptance/stubbing_non_public_class_method_test.rb
133
134
  - test/acceptance/stubbing_non_public_instance_method_test.rb
134
135
  - test/acceptance/stubbing_on_non_mock_object_test.rb
135
- - test/active_record_test_case.rb
136
136
  - test/deprecation_disabler.rb
137
137
  - test/execution_point.rb
138
138
  - test/method_definer.rb
@@ -219,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
219
  requirements: []
220
220
 
221
221
  rubyforge_project: mocha
222
- rubygems_version: 1.3.0
222
+ rubygems_version: 1.3.1
223
223
  signing_key:
224
224
  specification_version: 2
225
225
  summary: Mocking and stubbing library
@@ -1,36 +0,0 @@
1
- module ActiveRecordTestCase
2
-
3
- def setup_with_fixtures
4
- methods_called << :setup_with_fixtures
5
- end
6
-
7
- alias_method :setup, :setup_with_fixtures
8
-
9
- def teardown_with_fixtures
10
- methods_called << :teardown_with_fixtures
11
- end
12
-
13
- alias_method :teardown, :teardown_with_fixtures
14
-
15
- def self.method_added(method)
16
- case method.to_s
17
- when 'setup'
18
- unless method_defined?(:setup_without_fixtures)
19
- alias_method :setup_without_fixtures, :setup
20
- define_method(:setup) do
21
- setup_with_fixtures
22
- setup_without_fixtures
23
- end
24
- end
25
- when 'teardown'
26
- unless method_defined?(:teardown_without_fixtures)
27
- alias_method :teardown_without_fixtures, :teardown
28
- define_method(:teardown) do
29
- teardown_without_fixtures
30
- teardown_with_fixtures
31
- end
32
- end
33
- end
34
- end
35
-
36
- end