mocha 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +53 -37
- data/Rakefile +48 -0
- data/TODO +21 -0
- metadata +4 -2
data/README
CHANGED
@@ -1,24 +1,24 @@
|
|
1
1
|
= Mocha
|
2
2
|
|
3
|
-
Mocha is a library for mocking and stubbing
|
3
|
+
Mocha is a library for mocking and stubbing within tests using a syntax like that of JMock[http://www.jmock.org] and SchMock[http://rubyforge.org/projects/schmock].
|
4
4
|
|
5
5
|
Mocha comes in three parts:
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
1. Mocha - traditional mock objects with expectations and verification
|
8
|
+
2. Stubba - allows mocking and stubbing of methods on real (non-mock) classes
|
9
|
+
3. AutoMocha - magically provides mocks in the place of undefined classes
|
10
10
|
|
11
|
-
Stubba
|
11
|
+
Stubba and AutoMocha are the main difference between this mocking library and others like FlexMock[http://onestepback.org/software/flexmock] and RSpec[http://rspec.rubyforge.org].
|
12
12
|
|
13
13
|
== Provenance
|
14
14
|
|
15
|
-
Mocha
|
15
|
+
Mocha and Stubba have been created by amalgamating a number of techniques developed by me (James[http:blog.floehopper.org]) and my Reevoo[http://www.reevoo.com] colleagues (Ben[http://www.reevoo.com/blogs/bengriffiths/], Chris[http://blog.seagul.co.uk] and Paul[http://po-ru.com]) into a common syntax. They are both in use on real-world Rails[http://www.rubyonrails.org] projects. AutoMocha is more experimental and is at an earlier stage of development.
|
16
16
|
|
17
|
-
== Download
|
17
|
+
== Download and Installation
|
18
18
|
|
19
19
|
You can download Mocha from here[http://rubyforge.org/projects/mocha] or install Mocha with the following command.
|
20
20
|
|
21
|
-
$ gem install
|
21
|
+
$ gem install mocha
|
22
22
|
|
23
23
|
== License
|
24
24
|
|
@@ -26,7 +26,11 @@ Copyright Revieworld Ltd. 2006
|
|
26
26
|
|
27
27
|
You may use, copy and redistribute this library under the same terms as Ruby itself (see http://www.ruby-lang.org/en/LICENSE.txt).
|
28
28
|
|
29
|
-
==
|
29
|
+
== Examples
|
30
|
+
|
31
|
+
See MochaAcceptanceTest, StubbaAcceptanceTest, AutoMochaAcceptanceTest and unit tests for more examples.
|
32
|
+
|
33
|
+
=== Mocha Example
|
30
34
|
|
31
35
|
class Enterprise
|
32
36
|
|
@@ -40,7 +44,9 @@ You may use, copy and redistribute this library under the same terms as Ruby its
|
|
40
44
|
|
41
45
|
end
|
42
46
|
|
43
|
-
|
47
|
+
require 'rubygems'
|
48
|
+
require 'mocha'
|
49
|
+
require 'test/unit'
|
44
50
|
|
45
51
|
class EnterpriseTest < Test::Unit::TestCase
|
46
52
|
|
@@ -56,10 +62,12 @@ You may use, copy and redistribute this library under the same terms as Ruby its
|
|
56
62
|
|
57
63
|
end
|
58
64
|
|
59
|
-
|
65
|
+
=== Stubba Example
|
60
66
|
|
61
67
|
class Order
|
62
|
-
|
68
|
+
|
69
|
+
attr_accessor :shipped_on
|
70
|
+
|
63
71
|
def total_cost
|
64
72
|
line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
|
65
73
|
end
|
@@ -71,57 +79,62 @@ You may use, copy and redistribute this library under the same terms as Ruby its
|
|
71
79
|
def shipping_cost
|
72
80
|
total_weight * 5 + 10
|
73
81
|
end
|
74
|
-
|
82
|
+
|
75
83
|
class << self
|
76
84
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
85
|
+
def find_all
|
86
|
+
# Database.connection.execute('select * from orders...
|
87
|
+
end
|
88
|
+
|
89
|
+
def number_shipped_since(date)
|
90
|
+
find_all.select { |order| order.shipped_on > date }.size
|
91
|
+
end
|
92
|
+
|
93
|
+
def unshipped_value
|
94
|
+
find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
90
99
|
end
|
91
100
|
|
101
|
+
require 'rubygems'
|
92
102
|
require 'stubba'
|
103
|
+
require 'test/unit'
|
93
104
|
|
94
105
|
class OrderTest < Test::Unit::TestCase
|
95
106
|
|
96
107
|
# illustrates stubbing instance method
|
97
108
|
def test_should_calculate_shipping_cost_based_on_total_weight
|
98
|
-
|
99
|
-
|
100
|
-
|
109
|
+
order = Order.new
|
110
|
+
order.stubs(:total_weight).returns(10)
|
111
|
+
assert_equal 60, order.shipping_cost
|
101
112
|
end
|
102
113
|
|
103
114
|
# illustrates stubbing class method
|
104
115
|
def test_should_count_number_of_orders_shipped_after_specified_date
|
105
|
-
|
106
|
-
|
116
|
+
now = Time.now; week_in_secs = 7 * 24 * 60 * 60
|
117
|
+
order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
|
118
|
+
order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
|
107
119
|
Order.stubs(:find_all).returns([order_1, order_2])
|
108
|
-
assert_equal 1, Order.number_shipped_since(2
|
120
|
+
assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
|
109
121
|
end
|
110
122
|
|
111
123
|
# illustrates stubbing instance method for all instances of a class
|
112
124
|
def test_should_calculate_value_of_unshipped_orders
|
113
|
-
|
114
|
-
|
115
|
-
Order.any_instance.stubs(:shipped_on).returns(Time.now)
|
125
|
+
Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
|
126
|
+
Order.any_instance.stubs(:shipped_on).returns(nil)
|
116
127
|
Order.any_instance.stubs(:total_cost).returns(10)
|
117
|
-
assert_equal
|
128
|
+
assert_equal 30, Order.unshipped_value
|
118
129
|
end
|
119
130
|
|
120
131
|
end
|
121
132
|
|
122
|
-
|
133
|
+
=== AutoMocha Example
|
123
134
|
|
124
135
|
class Article
|
136
|
+
|
137
|
+
attr_reader :id
|
125
138
|
|
126
139
|
def accepted_comments
|
127
140
|
Comment.find_all_by_article_id(self.id).select { |comment| comment.accepted? }
|
@@ -129,7 +142,9 @@ You may use, copy and redistribute this library under the same terms as Ruby its
|
|
129
142
|
|
130
143
|
end
|
131
144
|
|
145
|
+
require 'rubygems'
|
132
146
|
require 'auto_mocha'
|
147
|
+
require 'test/unit'
|
133
148
|
|
134
149
|
class OrderTest < Test::Unit::TestCase
|
135
150
|
|
@@ -146,3 +161,4 @@ You may use, copy and redistribute this library under the same terms as Ruby its
|
|
146
161
|
end
|
147
162
|
|
148
163
|
end
|
164
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
|
5
|
+
desc "Default task is currently to run all tests"
|
6
|
+
task :default => :test_all
|
7
|
+
|
8
|
+
desc "Run all tests"
|
9
|
+
task :test_all do
|
10
|
+
$: << "#{File.dirname(__FILE__)}/test"
|
11
|
+
require 'test/all_tests'
|
12
|
+
end
|
13
|
+
|
14
|
+
desc 'Generate RDoc'
|
15
|
+
Rake::RDocTask.new do |task|
|
16
|
+
task.rdoc_dir = 'doc'
|
17
|
+
task.options << '--title Mocha --main README --line-numbers --inline-source'
|
18
|
+
task.rdoc_files.include('README', 'lib/**/*.rb')
|
19
|
+
end
|
20
|
+
|
21
|
+
Gem::manage_gems
|
22
|
+
|
23
|
+
specification = Gem::Specification.new do |s|
|
24
|
+
s.name = "mocha"
|
25
|
+
s.summary = "Mocking and stubbing library"
|
26
|
+
s.version = "0.1.2"
|
27
|
+
s.author = 'James Mead'
|
28
|
+
s.description = <<-EOF
|
29
|
+
Mocking and stubbing library with JMock/SchMock syntax, which allows mocking and stubbing of methods on real (non-mock) classes.
|
30
|
+
Includes auto-mocking which magically provides mocks for undefined classes, facilitating unit tests with no external dependencies.
|
31
|
+
EOF
|
32
|
+
s.email = 'mocha-developer@rubyforge.org'
|
33
|
+
s.homepage = 'http://mocha.rubyforge.org'
|
34
|
+
s.rubyforge_project = 'mocha'
|
35
|
+
|
36
|
+
s.has_rdoc = true
|
37
|
+
s.extra_rdoc_files = ['README', 'COPYING']
|
38
|
+
s.rdoc_options << '--title' << 'Mocha' << '--main' << 'README' << '--line-numbers'
|
39
|
+
|
40
|
+
s.autorequire = 'mocha'
|
41
|
+
s.files = FileList['{lib,test}/**/*.rb', '[A-Z]*'].to_a
|
42
|
+
s.test_file = "test/all_tests.rb"
|
43
|
+
end
|
44
|
+
|
45
|
+
Rake::GemPackageTask.new(specification) do |package|
|
46
|
+
package.need_zip = true
|
47
|
+
package.need_tar = true
|
48
|
+
end
|
data/TODO
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
- check examples in readme
|
2
|
+
- write rdoc for most important methods/classes e.g. expectation
|
3
|
+
- mock() method on test case to hide implementation detail of Mocha.new
|
4
|
+
- include functionality into test case using module include for more flexibility - instead of re-opening test/unit/test_case
|
5
|
+
- improve implementation of test case setup/teardown
|
6
|
+
- experienced problem when including a module with setup method into a TestCase derived class (something like nil.unstub_all in teardown)
|
7
|
+
- test for setting expectations on class methods (and instance methods?) from within TestCase#setup
|
8
|
+
- test for verifying expectations from within TestCase#teardown
|
9
|
+
- use Object#inspect(:mocha) instead of Object#mocha_inspect?
|
10
|
+
- allow stubbing of private/protected methods?
|
11
|
+
- should all instances share expectations for any_instance or should each instance have their own - in which case how do we provide access to the instances
|
12
|
+
- check if mocking 'new' method is still possible
|
13
|
+
- detect existing or added definition of mocha methods e.g. expects and alias to __expects?
|
14
|
+
- think more about different behaviour for expects/stubs
|
15
|
+
- auto-verify? default to on, allow switch off?
|
16
|
+
- fail if verify called with no expectations? set expectation - expects(:blah).never to ensure method not called
|
17
|
+
- fail if attempt to verify stub??
|
18
|
+
- maybe use blank_slate as mocha parent class to allow mocking of standard object methods?
|
19
|
+
- more jmock style stuff - e.g. return values on consecutive calls, labels/required order, more sophisticated param matching?
|
20
|
+
- stubs should only return a fixed value - no blocks allowed for return values and no parameter expectations allowed?
|
21
|
+
- maybe allow unstubbing of a specific method from within a test...?
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: mocha
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
6
|
+
version: 0.1.2
|
7
7
|
date: 2006-07-16 00:00:00 +01:00
|
8
8
|
summary: Mocking and stubbing library
|
9
9
|
require_paths:
|
@@ -72,8 +72,10 @@ files:
|
|
72
72
|
- test/stubba/object_test.rb
|
73
73
|
- test/stubba/stubba_test.rb
|
74
74
|
- test/stubba/test_case_test.rb
|
75
|
-
- README
|
76
75
|
- COPYING
|
76
|
+
- Rakefile
|
77
|
+
- README
|
78
|
+
- TODO
|
77
79
|
test_files:
|
78
80
|
- test/all_tests.rb
|
79
81
|
rdoc_options:
|