| 1 |
=begin rdoc |
| 2 |
= Usage |
| 3 |
Behaviors provides a single method: should. |
| 4 |
|
| 5 |
Instead of naming test methods like: |
| 6 |
|
| 7 |
def test_something |
| 8 |
end |
| 9 |
|
| 10 |
You declare test methods like: |
| 11 |
|
| 12 |
should "perform action" do |
| 13 |
end |
| 14 |
|
| 15 |
You may omit the body of a <tt>should</tt> method to describe unimplemented behavior. |
| 16 |
|
| 17 |
should "perform other action" |
| 18 |
|
| 19 |
When you run your unit tests, empty <tt>should</tt> methods will appear as an 'UNIMPLEMENTED CASE' along with the described behavior. |
| 20 |
This is useful for sketching out planned behavior quickly. |
| 21 |
|
| 22 |
Simply <tt>extend Behaviors</tt> in your <tt>TestCase</tt> to start using behaviors. |
| 23 |
|
| 24 |
require 'test/unit' |
| 25 |
require 'behaviors' |
| 26 |
require 'user' |
| 27 |
|
| 28 |
class UserTest < Test::Unit::TestCase |
| 29 |
extend Behaviors |
| 30 |
... |
| 31 |
end |
| 32 |
|
| 33 |
= Motivation |
| 34 |
Test methods typically focus on the name of the method under test instead of its behavior. |
| 35 |
Creating test methods with <tt>should</tt> statements focuses on the behavior of an object. |
| 36 |
This helps you to think about the role of the object under test. |
| 37 |
|
| 38 |
Using a behavior-driven approach prevents the danger in assuming a one-to-one mapping of method names to |
| 39 |
test method names. |
| 40 |
As always, you get the most value by writing the tests first. |
| 41 |
|
| 42 |
For a more complete BDD framework, try RSpec http://rspec.rubyforge.org/ |
| 43 |
|
| 44 |
= Rake tasks |
| 45 |
|
| 46 |
You can define a <tt>Behaviors::ReportTask</tt> in your <tt>Rakefile</tt> to generate rake tasks that |
| 47 |
summarize the behavior of your project. |
| 48 |
|
| 49 |
These tasks are named <tt>behaviors</tt> and <tt>behaviors_html</tt>. They will output to the |
| 50 |
console or an html file in the <tt>doc</tt> directory with a list all of your <tt>should</tt> tests. |
| 51 |
Behaviors::ReportTask.new do |t| |
| 52 |
t.pattern = 'test/**/*_test.rb' |
| 53 |
end |
| 54 |
|
| 55 |
You may also initialize the <tt>ReportTask</tt> with a custom name to associate with a particular suite of tests. |
| 56 |
Behaviors::ReportTask.new(:widget_subsystem) do |t| |
| 57 |
t.pattern = 'test/widgets/*_test.rb' |
| 58 |
end |
| 59 |
|
| 60 |
The html report will be placed in the <tt>doc</tt> directory by default. |
| 61 |
You can override this default by setting the <tt>html_dir</tt> in the <tt>ReportTask</tt>. |
| 62 |
Behaviors::ReportTask.new do |t| |
| 63 |
t.pattern = 'test/**/*_test.rb' |
| 64 |
t.html_dir = 'behaviors_html_reports' |
| 65 |
end |
| 66 |
=end |
| 67 |
module Behaviors |
| 68 |
def should(behave,&block) |
| 69 |
mname = "test_should_#{behave}" |
| 70 |
if block |
| 71 |
define_method mname, &block |
| 72 |
else |
| 73 |
puts ">>> UNIMPLEMENTED CASE: #{name.sub(/Test$/,'')} should #{behave}" |
| 74 |
end |
| 75 |
end |
| 76 |
end |