test_unit-given 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +59 -20
- data/lib/test/unit/given/simple.rb +3 -3
- data/lib/test/unit/given/test_case.rb +1 -4
- data/lib/test/unit/given/version.rb +1 -1
- data/test/test_circle.rb +17 -0
- data/test/test_simple_given.rb +1 -1
- metadata +73 -54
data/README.rdoc
CHANGED
@@ -58,17 +58,14 @@ You can, of course, provide a description for your test if you need to:
|
|
58
58
|
assert_equal 314,@area
|
59
59
|
}
|
60
60
|
end
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
require 'test/unit/given/simple'
|
63
|
+
You can use as much, or as little, of this library as you want. If you don't like +test_that+, it's no problem:
|
65
64
|
|
66
65
|
class CircleTest < Test::Unit::TestCase
|
67
|
-
|
68
66
|
include Test::Unit::Given::Simple
|
69
|
-
include Test::Unit::Given::TestThat
|
70
67
|
|
71
|
-
|
68
|
+
def test_that_area_is_calculated
|
72
69
|
Given {
|
73
70
|
@circle = Circle.new(10)
|
74
71
|
}
|
@@ -78,24 +75,58 @@ If you don't want to extend our base class, you can mix in the features explicit
|
|
78
75
|
Then {
|
79
76
|
assert_equal 314,@area
|
80
77
|
}
|
81
|
-
|
78
|
+
end
|
82
79
|
end
|
83
80
|
|
81
|
+
If you just want to use the +Given+, you can:
|
84
82
|
|
85
|
-
|
83
|
+
class CircleTest < Test::Unit::TestCase
|
84
|
+
include Test::Unit::Given::Simple
|
85
|
+
|
86
|
+
def test_that_area_is_calculated
|
87
|
+
Given {
|
88
|
+
@circle = Circle.new(10)
|
89
|
+
}
|
90
|
+
area = @circle.area
|
91
|
+
assert_equal 314,@area
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
Feel a +Given+ is too verbose?
|
96
|
+
|
97
|
+
class CircleTest < Test::Unit::TestCase
|
98
|
+
include Test::Unit::Given::Simple
|
99
|
+
|
100
|
+
def test_that_area_is_calculated
|
101
|
+
When {
|
102
|
+
@area = Circle.new(10).area
|
103
|
+
}
|
104
|
+
Then {
|
105
|
+
assert_equal 314,@area
|
106
|
+
}
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Use whatever makes sense; this is here to make your tests readable and communicate your intent, *not* lock you into some particular way of writing your tests.
|
111
|
+
|
112
|
+
== How does it work?
|
113
|
+
|
114
|
+
+Given+/+When+/+Then+/+And+/+But+ are all the same method under the covers. They take a block and execute it immediately. By using instance variables, you can send information between blocks. This is actually a feature, since it means than any instance variables are important while local variables are just there to set up your test or help evalulate things.
|
115
|
+
|
116
|
+
This means that you can make methods that return blocks as a means of re-use.
|
86
117
|
|
87
118
|
class CircleTest < Test::Unit::Given::TestCase
|
88
119
|
|
89
120
|
def circle_with_radius(r)
|
90
|
-
@circle = Circle.new(r)
|
121
|
+
lambda { @circle = Circle.new(r) }
|
91
122
|
end
|
92
123
|
|
93
124
|
def get_area
|
94
|
-
@area = @circle.area
|
125
|
+
lambda { @area = @circle.area }
|
95
126
|
end
|
96
127
|
|
97
128
|
def area_should_be(area)
|
98
|
-
assert_equal area,@area
|
129
|
+
lambda { assert_equal area,@area }
|
99
130
|
end
|
100
131
|
|
101
132
|
test_that {
|
@@ -111,6 +142,10 @@ Finally, you can re-use blocks, and use And to create richer expressions:
|
|
111
142
|
}
|
112
143
|
end
|
113
144
|
|
145
|
+
I would not recommend doing this a lot, as it can make things very confusing. You could just as easily continue to use methods.
|
146
|
+
|
147
|
+
+test_that+ works just like +test+ in Rails, except that it doesn't require a name. If your test is short enough, naming it might make things more confusing. I tend to always name mine, but on occasion it gets in the way.
|
148
|
+
|
114
149
|
=== What about mocks?
|
115
150
|
|
116
151
|
Mocks create an interesting issue, because the "assertions" are the mock expectations you setup before you call the method under test. This means that the "then" side of things is out of order.
|
@@ -120,10 +155,10 @@ Mocks create an interesting issue, because the "assertions" are the mock expecta
|
|
120
155
|
Given {
|
121
156
|
@diameter_service = mock()
|
122
157
|
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
123
|
-
@
|
158
|
+
@circle = Circle.new(10,@diameter_service)
|
124
159
|
}
|
125
160
|
When {
|
126
|
-
@diameter = @
|
161
|
+
@diameter = @circle.diameter
|
127
162
|
}
|
128
163
|
Then {
|
129
164
|
// assume mocks were called
|
@@ -131,42 +166,46 @@ Mocks create an interesting issue, because the "assertions" are the mock expecta
|
|
131
166
|
end
|
132
167
|
end
|
133
168
|
|
134
|
-
This is somewhat confusing. We could solve it like so:
|
169
|
+
This is somewhat confusing. We could solve it using two blocks provided by this library, +test_runs+, and +mocks_shouldve_been_called+, like so:
|
135
170
|
|
136
171
|
class CircleTest < Test::Unit::Given::TestCase
|
137
172
|
test_that "our external diameter service is being used" do
|
138
173
|
Given {
|
139
174
|
@diameter_service = mock()
|
140
175
|
}
|
141
|
-
When
|
176
|
+
When test_runs
|
142
177
|
Then {
|
143
178
|
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
144
179
|
}
|
145
180
|
Given {
|
146
|
-
@
|
181
|
+
@circle = Circle.new(10,@diameter_service)
|
147
182
|
}
|
148
183
|
When {
|
149
|
-
@diameter = @
|
184
|
+
@diameter = @circle.diameter
|
150
185
|
}
|
151
186
|
Then mocks_shouldve_been_called
|
152
187
|
end
|
153
188
|
end
|
154
189
|
|
155
|
-
Although both <tt>
|
190
|
+
Although both <tt>test_runs</tt> and <tt>mocks_shouldve_been_called</tt> are no-ops,
|
156
191
|
they allow our tests to be readable and make clear what the assertions are that we are making.
|
157
192
|
|
193
|
+
Yes, this makes our test a bit longer, but it's *much* more clear.
|
194
|
+
|
158
195
|
=== What about block-based assertions, like +assert_raises+
|
159
196
|
|
197
|
+
Again, things are a bit out of order, but if you invert Then and When, you'll still get a readable test:
|
198
|
+
|
160
199
|
class CircleTest < Test::Unit::Given::TestCase
|
161
200
|
|
162
201
|
test_that "there is no diameter method" do
|
163
202
|
Given {
|
164
|
-
@
|
203
|
+
@circle = Circle.new(10)
|
165
204
|
}
|
166
205
|
Then {
|
167
206
|
assert_raises NoMethodError do
|
168
207
|
When {
|
169
|
-
@
|
208
|
+
@circle.diameter
|
170
209
|
}
|
171
210
|
end
|
172
211
|
}
|
@@ -136,7 +136,7 @@ module Test
|
|
136
136
|
# Given {
|
137
137
|
# @google = mock()
|
138
138
|
# }
|
139
|
-
# When
|
139
|
+
# When test_runs
|
140
140
|
# Then {
|
141
141
|
# @google.expects(:search).with('foo').returns('bar')
|
142
142
|
# }
|
@@ -150,11 +150,11 @@ module Test
|
|
150
150
|
# assert_equal 'Found bar',@result
|
151
151
|
# }
|
152
152
|
# And mocks_shouldve_been_called
|
153
|
-
def
|
153
|
+
def test_runs
|
154
154
|
lambda {}
|
155
155
|
end
|
156
156
|
|
157
|
-
# Similar to #
|
157
|
+
# Similar to #test_runs, this is used to make clear what
|
158
158
|
# you are testing and what the assertions are. Since many Ruby mock
|
159
159
|
# frameworks do not require an explicit "verify" step, you often have tests
|
160
160
|
# that have no explicit asserts, the assertions being simply that the mocks were called
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'test/unit'
|
1
2
|
require 'test/unit/given/simple'
|
2
3
|
require 'test/unit/given/test_that'
|
3
4
|
|
@@ -20,10 +21,6 @@ module Test
|
|
20
21
|
class TestCase < Test::Unit::TestCase
|
21
22
|
include Simple
|
22
23
|
include TestThat
|
23
|
-
|
24
|
-
def test_nothing_for_187
|
25
|
-
# 1.8.7 seems to need this
|
26
|
-
end
|
27
24
|
end
|
28
25
|
end
|
29
26
|
end
|
data/test/test_circle.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit/given'
|
2
|
+
|
3
|
+
class Circle
|
4
|
+
def initialize(radius)
|
5
|
+
@radius = radius
|
6
|
+
end
|
7
|
+
|
8
|
+
def area; (3.14 * @radius * @radius).to_i; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestCircle < Test::Unit::Given::TestCase
|
12
|
+
test_that {
|
13
|
+
Given { @circle = Circle.new(10) }
|
14
|
+
When { @area = @circle.area }
|
15
|
+
Then { assert_equal 314,@area }
|
16
|
+
}
|
17
|
+
end
|
data/test/test_simple_given.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,68 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_unit-given
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 1
|
9
|
+
version: 0.9.1
|
6
10
|
platform: ruby
|
7
|
-
authors:
|
11
|
+
authors:
|
8
12
|
- David Copeland
|
9
13
|
autorequire:
|
10
14
|
bindir: bin
|
11
15
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
|
17
|
+
date: 2011-11-20 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
15
21
|
name: rdoc
|
16
|
-
requirement: &70252391815780 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
22
|
prerelease: false
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '0'
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
33
30
|
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rake
|
34
34
|
prerelease: false
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
44
42
|
type: :development
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: simplecov
|
45
46
|
prerelease: false
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id003
|
56
|
+
description: We don't need no stinkin' RSpec! Get all the fluency you want in your Test::Unit tests, with no magic required, using straight Ruby syntax
|
57
|
+
email:
|
50
58
|
- davetron5000@gmail.com
|
51
59
|
executables: []
|
60
|
+
|
52
61
|
extensions: []
|
62
|
+
|
53
63
|
extra_rdoc_files: []
|
54
|
-
|
64
|
+
|
65
|
+
files:
|
55
66
|
- .gitignore
|
56
67
|
- .rvmrc
|
57
68
|
- Gemfile
|
@@ -64,34 +75,42 @@ files:
|
|
64
75
|
- lib/test/unit/given/test_that.rb
|
65
76
|
- lib/test/unit/given/version.rb
|
66
77
|
- test/bootstrap.rb
|
78
|
+
- test/test_circle.rb
|
67
79
|
- test/test_simple_given.rb
|
68
80
|
- test/test_test_that.rb
|
69
81
|
- test_unit-given.gemspec
|
70
|
-
|
82
|
+
has_rdoc: true
|
83
|
+
homepage: ""
|
71
84
|
licenses: []
|
85
|
+
|
72
86
|
post_install_message:
|
73
87
|
rdoc_options: []
|
74
|
-
|
88
|
+
|
89
|
+
require_paths:
|
75
90
|
- lib
|
76
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
requirements:
|
85
|
-
- -
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
88
105
|
requirements: []
|
106
|
+
|
89
107
|
rubyforge_project: test_unit-given
|
90
|
-
rubygems_version: 1.
|
108
|
+
rubygems_version: 1.3.6
|
91
109
|
signing_key:
|
92
110
|
specification_version: 3
|
93
111
|
summary: Use Given/When/Then in your Test::Unit tests
|
94
|
-
test_files:
|
112
|
+
test_files:
|
95
113
|
- test/bootstrap.rb
|
114
|
+
- test/test_circle.rb
|
96
115
|
- test/test_simple_given.rb
|
97
116
|
- test/test_test_that.rb
|