-
Notifications
You must be signed in to change notification settings - Fork 3
Writing Tests
Let’s say we have a library hello_world.rb
that consists of the class HelloWorld
with method #to_s
:
class HelloWorld
def to_s
"Hello, World!"
end
end
We would write a Lemon test case along the following lines:
Covers 'hello_world'
TestClass HelloWorld do
Unit :to_s do
Test "returns the world famous phrase" do
HelloWorld.new.to_s.assert == "Hello, World!"
end
end
end
Clearly this an overly simplistic example, but it demonstrates well enough the basic design of Lemon tests. Notice that the #TestCase
method references a class (this can also be a module) and the units each refer to a method of the class.
Lemon uses the Assertive Expressive assertions framework. This is the same versitile framework used by Q.E.D.. By default Lemon tests support the standard #assert
and #expect
assertion methods. If you wish to use subjunctive terms, either #should
or #must
, you can load these via a helper script (eg. require 'ae/should'
).
Lemon provides a concern method #Concern
, and alias #Setup
method, as before advice for each
test. Using this, the example given at the top of the page could be written:
Covers 'hello_world'
TestClass HelloWorld do
Concern "String output works as expected." do
@hw = HelloWord.new
end
Unit :to_s do
Test "returns the world famous phrase" do
@hw.to_s.assert == "Hello, World!"
end
end
end
To complement the concern methods, Lemon provides #Teardown
, which can be used to run a procedure after each unit test.
The #Before
and #After
methods can be used to define specialized advice procedures, triggered my pattern matching to method name and/or test descriptions.
Capitalized method names for Lemon’s syntax were originally chosen to help ensure any name clashes with code to be tested. At the time the methods were defined in the Kernel and tests were evaluated at the toplevel, so it seemed a prudent precaution. The current implementation runs the tests within the scope of a special object (TestSuite::DSL
), so there is no potential for name clash. For this reason, lower case variety of the name methods have been made available in the newest release of Lemon. For example:
covers 'hello_world'
test_class HelloWorld do
concern "String output works as expected." do
HelloWord.new
end
unit :to_s do
test "returns the world famous phrase" do
@hw.to_s.assert == "Hello, World!"
end
end
end
Even though we can now use lower-case method names, per the usual Ruby practice, I have become accustomed to the upper-case variety at this point, so have no to intention of deprecating them. Use which ever you prefer.