clean_test 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/.rvmrc +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +201 -0
- data/README.rdoc +343 -0
- data/Rakefile +29 -0
- data/clean_test.gemspec +25 -0
- data/lib/clean_test/any.rb +169 -0
- data/lib/clean_test/given_when_then.rb +186 -0
- data/lib/clean_test/test_case.rb +32 -0
- data/lib/clean_test/test_that.rb +65 -0
- data/lib/clean_test/version.rb +5 -0
- data/test/bootstrap.rb +5 -0
- data/test/test_any.rb +144 -0
- data/test/test_circle.rb +17 -0
- data/test/test_simple_given.rb +107 -0
- data/test/test_test_that.rb +35 -0
- metadata +114 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@clean_test --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.rdoc
ADDED
@@ -0,0 +1,343 @@
|
|
1
|
+
= Clean Tests
|
2
|
+
|
3
|
+
Author:: Dave Copeland (mailto:davetron5000 at g mail dot com)
|
4
|
+
Copyright:: Copyright (c) 2012 by Dave Copeland
|
5
|
+
License:: Distributes under the Apache License, see LICENSE.txt in the source distro
|
6
|
+
|
7
|
+
Get your Test::Unit test cases readable and fluent, without RSpec, magic, or crazy meta-programming.
|
8
|
+
|
9
|
+
This library is a set of small, simple tools to make your Test::Unit test cases easy to understand. This isn't a massive change in how you write tests, but simply some helpful things will make your tests easier to read.
|
10
|
+
|
11
|
+
The main problems this library solves are:
|
12
|
+
|
13
|
+
* Understanding what part of a test method is setup, test, and evaluation
|
14
|
+
* Understanding what elements of a test are relevant to the test, and which are arbitrary placeholders
|
15
|
+
* Removing the requirement that your tests are method names
|
16
|
+
|
17
|
+
<b>Note: this gem is still called <tt>test_unit-given</tt> in RubyGems; I'm in the middle of changing the name</b>
|
18
|
+
|
19
|
+
== Install
|
20
|
+
|
21
|
+
gem install clean_test
|
22
|
+
|
23
|
+
Or, with bundler:
|
24
|
+
|
25
|
+
gem "clean_test", :require => false
|
26
|
+
|
27
|
+
== Overview
|
28
|
+
|
29
|
+
class Circle
|
30
|
+
attr_reader :name
|
31
|
+
attr_reader :radius
|
32
|
+
|
33
|
+
def initialize(radius,name)
|
34
|
+
@radius = radius
|
35
|
+
@name = name
|
36
|
+
end
|
37
|
+
|
38
|
+
def area
|
39
|
+
@radius * @radius * 3.14
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
"circle of radius #{radius}, named #{name}"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
require 'clean_test/test_case'
|
49
|
+
|
50
|
+
class CircleTest < Clean::Test::TestCase
|
51
|
+
test_that "area is computed correctly" {
|
52
|
+
Given {
|
53
|
+
@circle = Circle.new(10,any_string)
|
54
|
+
}
|
55
|
+
When {
|
56
|
+
@area = @circle.area
|
57
|
+
}
|
58
|
+
Then {
|
59
|
+
assert_equal 314,@area
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
test_that "to_s includes the name" {
|
64
|
+
Given {
|
65
|
+
@name = "foo"
|
66
|
+
@circle = Circle.new(any_int,@name)
|
67
|
+
}
|
68
|
+
When {
|
69
|
+
@string = @circle.to_s
|
70
|
+
}
|
71
|
+
Then {
|
72
|
+
assert_match /#{@name}/,@string
|
73
|
+
}
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
What's going on here?
|
78
|
+
|
79
|
+
* We can clearly see which parts of our test are setting things up (stuff inside +Given+), which parts are executing the code we're testing (stuff in +When+) and which parts are evalulating the results (stuff in +Then+)
|
80
|
+
* We can see which values are relevant to the test - only those that are literals. In the first test, the +name+ of our circle is not relevant to the test, so instead of using a dummy value like <tt>"foo"</tt>, we use +any_string+, which makes it clear that the value <i>does not matter</i>. Similarly, in the second test, the radius is irrelevant, so we use +any_int+ to signify that it doesn't matter.
|
81
|
+
* Our tests are clearly named and described with strings, but we didn't need to bring in active support.
|
82
|
+
* A side effect of this structure is that we use instance vars to pass data between Given/When/Then blocks. This means that instance vars "jump out" as important variables to the test; non-instance vars "fade away" into the background.
|
83
|
+
|
84
|
+
But, don't fret, this is not an all-or-nothing proposition. Use whichever parts you like. Each feature is in a module that you can include as needed, or you can do what we're doing here and extend Clean::Test::TestCase to get everything at once.
|
85
|
+
|
86
|
+
== More Info
|
87
|
+
|
88
|
+
* Clean::Test::TestCase is the base class that gives you everything
|
89
|
+
* Clean::Test::GivenWhenThen provides the Given/When/Then construct
|
90
|
+
* Clean::Test::TestThat provides +test_that+
|
91
|
+
* Clean::Test::Any provides the +any_string+ and friends.
|
92
|
+
|
93
|
+
== Questions you might have
|
94
|
+
|
95
|
+
=== Why?
|
96
|
+
|
97
|
+
I'm tired of unreadable tests. Tests should be good, clean code, and it shoud be easy to see what's being tested. This is especially important when there is a lot of setup required to simulate something.
|
98
|
+
|
99
|
+
I also don't believe we need to resort ot a lot of metaprogramming tricks just to get our tests in this shape. RSpec, for example, creates strange constructs for things that are much more straightforward in plain Ruby. I like Test::Unit, and with just a bit of helper methods, we can make nice, readable tests, using just Ruby.
|
100
|
+
|
101
|
+
=== But the test methods are longer!
|
102
|
+
|
103
|
+
And? I don't mind a test method that's a bit longer if that makes it easy to understand. Certainly, a method like this is short:
|
104
|
+
|
105
|
+
|
106
|
+
def test_radius
|
107
|
+
assert_equal 314,Circle.new(10).radius
|
108
|
+
end
|
109
|
+
|
110
|
+
But, we rarely get such simple methods *and* this test method isn't very modifiable; everything is on one line and it doesn't encourage re-use. We can do better.
|
111
|
+
|
112
|
+
=== What about mocks?
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
class CircleTest < Test::Unit::Given::TestCase
|
117
|
+
test_that "our external diameter service is being used" do
|
118
|
+
Given {
|
119
|
+
@diameter_service = mock()
|
120
|
+
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
121
|
+
@circle = Circle.new(10,@diameter_service)
|
122
|
+
}
|
123
|
+
When {
|
124
|
+
@diameter = @circle.diameter
|
125
|
+
}
|
126
|
+
Then {
|
127
|
+
// assume mocks were called
|
128
|
+
}
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
This is somewhat confusing. We could solve it using two blocks provided by this library, +the_test_runs+, and +mocks_shouldve_been_called+, like so:
|
133
|
+
|
134
|
+
class CircleTest < Test::Unit::Given::TestCase
|
135
|
+
test_that "our external diameter service is being used" do
|
136
|
+
Given {
|
137
|
+
@diameter_service = mock()
|
138
|
+
}
|
139
|
+
When the_test_runs
|
140
|
+
Then {
|
141
|
+
@diameter_service.expects(:get_diameter).with(10).returns(400)
|
142
|
+
}
|
143
|
+
Given {
|
144
|
+
@circle = Circle.new(10,@diameter_service)
|
145
|
+
}
|
146
|
+
When {
|
147
|
+
@diameter = @circle.diameter
|
148
|
+
}
|
149
|
+
Then mocks_shouldve_been_called
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
Although both <tt>the_test_runs</tt> and <tt>mocks_shouldve_been_called</tt> are no-ops,
|
154
|
+
they allow our tests to be readable and make clear what the assertions are that we are making.
|
155
|
+
|
156
|
+
Yes, this makes our test a bit longer, but it's *much* more clear.
|
157
|
+
|
158
|
+
=== What about block-based assertions, like +assert_raises+
|
159
|
+
|
160
|
+
Again, things are a bit out of order in a class test case, but you can clean this up without this library or any craziness, by just using Ruby:
|
161
|
+
|
162
|
+
class CircleTest < Clean::Test::TestCase
|
163
|
+
|
164
|
+
test_that "there is no diameter method" do
|
165
|
+
Given {
|
166
|
+
@circle = Circle.new(10)
|
167
|
+
}
|
168
|
+
When {
|
169
|
+
@code = lambda { @circle.diameter }
|
170
|
+
}
|
171
|
+
Then {
|
172
|
+
assert_raises(NoMethodError,&@code)
|
173
|
+
}
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
=== My tests require a lot of setup, so I use contexts in shoulda/RSpec. What say you?
|
178
|
+
|
179
|
+
Duplicated setup can be tricky. A problem with heavily nested contexts in Shoulda or RSpec is that it can be hard to piece together what all the "Givens" of a particular test actually are. As a reaction to this, a lot of developers tend to just duplicate setup code, so that each test "stands on its own". This makes adding features or changing things difficult, because it's not clear what duplicated code is the same by happenstance, or the same because it's *supposed* to be the same.
|
180
|
+
|
181
|
+
To deal with this, we simply use Ruby and <i>method extraction</i>. Let's say we have a +Salutation+ class that takes a +Person+ and a +Language+ in its constructor, and then provides methods to "greet" that person
|
182
|
+
|
183
|
+
class Salutation
|
184
|
+
def initialize(person,language)
|
185
|
+
raise "person required" if person.nil?
|
186
|
+
raise "language required" if language.nil?
|
187
|
+
end
|
188
|
+
|
189
|
+
# ... methods
|
190
|
+
end
|
191
|
+
|
192
|
+
To test this class, we always need a non-nil person and language. We might end up with code like this:
|
193
|
+
|
194
|
+
class SalutationTest << Clean::Test::TestCase
|
195
|
+
test_that "greeting works" do
|
196
|
+
Given {
|
197
|
+
person = Person.new("David","Copeland",:male)
|
198
|
+
language = Language.new("English","en")
|
199
|
+
@salutation = Salutation.new(person,language)
|
200
|
+
}
|
201
|
+
When {
|
202
|
+
@greeting = @salutation.greeting
|
203
|
+
}
|
204
|
+
Then {
|
205
|
+
assert_equal "Hello, David!",@salutation.greeting
|
206
|
+
}
|
207
|
+
end
|
208
|
+
|
209
|
+
test_that "greeting works for no first name" do
|
210
|
+
Given {
|
211
|
+
person = Person.new(nil,"Copeland",:male)
|
212
|
+
language = Language.new("English","en")
|
213
|
+
@salutation = Salutation.new(person,language)
|
214
|
+
}
|
215
|
+
When {
|
216
|
+
@greeting = @salutation.greeting
|
217
|
+
}
|
218
|
+
Then {
|
219
|
+
assert_equal "Hello, Mr. Copeland!",@salutation.greeting
|
220
|
+
}
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
In both cases, the language is the same, and the person is slightly different. Method extraction:
|
225
|
+
|
226
|
+
class SalutationTest << Clean::Test::TestCase
|
227
|
+
test_that "greeting works" do
|
228
|
+
Given {
|
229
|
+
@salutation = Salutation.new(male_with_first_name("David"),english)
|
230
|
+
}
|
231
|
+
When {
|
232
|
+
@greeting = @salutation.greeting
|
233
|
+
}
|
234
|
+
Then {
|
235
|
+
assert_equal "Hello, David!",@salutation.greeting
|
236
|
+
}
|
237
|
+
end
|
238
|
+
|
239
|
+
test_that "greeting works for no first name" do
|
240
|
+
Given {
|
241
|
+
@salutation = Salutation.new(male_with_no_first_name("Copeland"),english)
|
242
|
+
}
|
243
|
+
When {
|
244
|
+
@greeting = @salutation.greeting
|
245
|
+
}
|
246
|
+
Then {
|
247
|
+
assert_equal "Hello, Mr. Copeland!",@salutation.greeting
|
248
|
+
}
|
249
|
+
end
|
250
|
+
|
251
|
+
private
|
252
|
+
def male_with_first_name(first_name)
|
253
|
+
Person.new(first_name,any_string,:male)
|
254
|
+
end
|
255
|
+
|
256
|
+
def male_with_no_first_name(last_name)
|
257
|
+
Person.new(nil,last_name,:male)
|
258
|
+
end
|
259
|
+
|
260
|
+
def english; Language.new("English","en"); end
|
261
|
+
end
|
262
|
+
|
263
|
+
=== What did that have to do with this gem?
|
264
|
+
|
265
|
+
Nothing. That's the point. You have the power already. Note that, since +Given+ takes a block, you can re-use things that way, if you like:
|
266
|
+
|
267
|
+
class SalutationTest << Clean::Test::TestCase
|
268
|
+
test_that "greeting works" do
|
269
|
+
Given english_salutation_for(male_with_first_name("David")
|
270
|
+
When {
|
271
|
+
@greeting = @salutation.greeting
|
272
|
+
}
|
273
|
+
Then {
|
274
|
+
assert_equal "Hello, David!",@salutation.greeting
|
275
|
+
}
|
276
|
+
end
|
277
|
+
|
278
|
+
test_that "greeting works for no first name" do
|
279
|
+
Given english_salutation_for(male_with_first_name("Copeland")
|
280
|
+
When {
|
281
|
+
@greeting = @salutation.greeting
|
282
|
+
}
|
283
|
+
Then {
|
284
|
+
assert_equal "Hello, Mr. Copeland!",@salutation.greeting
|
285
|
+
}
|
286
|
+
end
|
287
|
+
|
288
|
+
private
|
289
|
+
def male_with_first_name(first_name)
|
290
|
+
Person.new(first_name,any_string,:male)
|
291
|
+
end
|
292
|
+
|
293
|
+
def male_with_no_first_name(last_name)
|
294
|
+
Person.new(nil,last_name,:male)
|
295
|
+
end
|
296
|
+
|
297
|
+
def english_salutation_for(person)
|
298
|
+
lambda {
|
299
|
+
@salutation = Salutation.new(person,Language.new("English","en"))
|
300
|
+
}
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
This sort of thing can get confusing, but sometimes works well.
|
305
|
+
|
306
|
+
=== Why Any instead of Faker?
|
307
|
+
|
308
|
+
Faker is used by Any under the covers, but Faker has two problems:
|
309
|
+
|
310
|
+
* We aren't _faking_ values, we're using _arbitrary_ values. There's a difference semantically, even if the mechanics are the same
|
311
|
+
* Faker requires too much typing to get arbitrary values. I'd rather type +any_string+ than <tt>Faker::Lorem.words(1).join(' ')</tt>
|
312
|
+
|
313
|
+
=== What about Factory Girl?
|
314
|
+
|
315
|
+
Again, FactoryGirl goes through metaprogramming hoops to do something we can already do in Ruby: call methods. Factory Girl also places factories in global scope, making tests more brittle. You either have a ton of tests depending on the same factory or you have test-specific factories, all in global scope. It's just simpler and more maintainable to use methods and modules for this. To re-use "factories" produced by simple methods, just put them in a module.
|
316
|
+
|
317
|
+
Further, the +Any+ module is extensible, in that you can do stuff like <tt>any Person</tt>, but you can, and should, just use methods. Any helps out with primitives that we tend to use a lot: numbers and strings. It's just simpler and, with less moving parts, more predictable. This means you spend more time on your tests than on your test infrastructure.
|
318
|
+
|
319
|
+
=== What about not using the base class?
|
320
|
+
|
321
|
+
To use Any on its own:
|
322
|
+
|
323
|
+
require 'clean_test/any'
|
324
|
+
|
325
|
+
class MyTest < Test::Unit::TestCase
|
326
|
+
include Clean::Test::Any
|
327
|
+
end
|
328
|
+
|
329
|
+
To use GivenWhenThen on its own:
|
330
|
+
|
331
|
+
require 'clean_test/given_when_then'
|
332
|
+
|
333
|
+
class MyTest < Test::Unit::TestCase
|
334
|
+
include Clean::Test::GivenWhenThen
|
335
|
+
end
|
336
|
+
|
337
|
+
To use TestThat on its own:
|
338
|
+
|
339
|
+
require 'clean_test/test_that'
|
340
|
+
|
341
|
+
class MyTest < Test::Unit::TestCase
|
342
|
+
include Clean::Test::TestThat
|
343
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
gem 'rdoc' # I need to use the installed RDoc gem, not what comes with the system
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rdoc/task'
|
6
|
+
|
7
|
+
include Rake::DSL
|
8
|
+
|
9
|
+
Bundler::GemHelper.install_tasks
|
10
|
+
|
11
|
+
desc 'run tests'
|
12
|
+
Rake::TestTask.new do |t|
|
13
|
+
t.libs << "lib"
|
14
|
+
t.libs << "test"
|
15
|
+
t.ruby_opts << "-rrubygems"
|
16
|
+
t.test_files = FileList['test/bootstrap.rb','test/test_*.rb']
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'build rdoc'
|
20
|
+
RDoc::Task.new do |rd|
|
21
|
+
rd.main = "README.rdoc"
|
22
|
+
rd.generator = 'hanna'
|
23
|
+
rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
|
24
|
+
rd.title = 'Test::Unit::Given - make your unit tests clear'
|
25
|
+
rd.markup = "tomdoc"
|
26
|
+
end
|
27
|
+
CLOBBER << 'coverage'
|
28
|
+
|
29
|
+
task :default => :test
|
data/clean_test.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "clean_test/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "clean_test"
|
7
|
+
s.version = Clean::Test::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["David Copeland"]
|
10
|
+
s.email = ["davetron5000@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Clean up your Test::Unit tests}
|
13
|
+
s.description = %q{You can easily make your plain Ruby Test::Unit test cases clean and clear with Given/When/Then, placeholder values, and textual descriptions without resorting to metaprogramming or complex frameworks. Use as much or as little as you like}
|
14
|
+
|
15
|
+
s.rubyforge_project = "clean_test"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_dependency("faker")
|
22
|
+
s.add_development_dependency("rdoc")
|
23
|
+
s.add_development_dependency("rake")
|
24
|
+
s.add_development_dependency("simplecov")
|
25
|
+
end
|