behaviors 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/behaviors.rb +50 -0
- data/test/behaviors_tasks_test.rb +71 -0
- data/test/behaviors_test.rb +50 -0
- data/test/tasks_test/Rakefile +16 -0
- data/test/tasks_test/doc/behaviors.html +55 -0
- data/test/tasks_test/lib/user.rb +2 -0
- data/test/tasks_test/test/user_test.rb +17 -0
- metadata +55 -0
data/lib/behaviors.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
=begin rdoc
|
2
|
+
= Usage
|
3
|
+
Behaviors provides a single method: should.
|
4
|
+
|
5
|
+
Your test classes should <tt>extend Behaviors</tt>.
|
6
|
+
|
7
|
+
Instead of naming test methods like:
|
8
|
+
|
9
|
+
def test_something
|
10
|
+
end
|
11
|
+
|
12
|
+
You declare test methods like:
|
13
|
+
|
14
|
+
should "perform action" do
|
15
|
+
end
|
16
|
+
|
17
|
+
You also have the ability to declare flunking test methods as a way
|
18
|
+
to describe future tests:
|
19
|
+
|
20
|
+
should "perform other action"
|
21
|
+
|
22
|
+
= Motivation
|
23
|
+
Test methods typically focus on the name of the method under test instead of its behavior.
|
24
|
+
|
25
|
+
Creating test methods with <tt>should</tt> statements focuses on the behaviors of an object.
|
26
|
+
This enhances the TDD experience by provoking the developer to think about the role of the object under test.
|
27
|
+
|
28
|
+
Writing the tests first to declare an object's behaviors and then implementing those
|
29
|
+
behaviors through object methods produces the most value.
|
30
|
+
Using this behavior-driven approach prevents the dangers associated with assuming a one-to-one mapping of method names to
|
31
|
+
test method names.
|
32
|
+
|
33
|
+
For a more complete BDD framework, try RSpec http://rspec.rubyforge.org/
|
34
|
+
|
35
|
+
= Rake tasks
|
36
|
+
|
37
|
+
Behaviors includes a pair of Rake tasks, <tt>behaviors</tt> and <tt>behaviors_html</tt>. These tasks will output to the
|
38
|
+
console or an html file in the <tt>doc</tt> directory with a list all of your <tt>should</tt> tests.
|
39
|
+
Use these tasks to summarize the behavior of your project.
|
40
|
+
=end
|
41
|
+
module Behaviors
|
42
|
+
def should(behave,&block)
|
43
|
+
mname = "test_should_#{behave}"
|
44
|
+
if block
|
45
|
+
define_method mname, &block
|
46
|
+
else
|
47
|
+
puts ">>> UNIMPLEMENTED CASE: #{name.sub(/Test$/,'')} should #{behave}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
|
4
|
+
|
5
|
+
class BehaviorsTasksTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
here = File.expand_path(File.dirname(__FILE__))
|
9
|
+
cmd = RUBY_PLATFORM[/mswin/] ? 'rake.cmd' : 'rake'
|
10
|
+
@base_cmd = "#{cmd} -f \"#{here}/tasks_test/Rakefile\" "
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# HELPERS
|
15
|
+
#
|
16
|
+
def run_behaviors_task
|
17
|
+
run_cmd "behaviors"
|
18
|
+
end
|
19
|
+
|
20
|
+
def run_behaviors_html_task
|
21
|
+
run_cmd "behaviors_html"
|
22
|
+
end
|
23
|
+
|
24
|
+
def run_cmd(cmd)
|
25
|
+
@report = %x[ #{@base_cmd} #{cmd} ]
|
26
|
+
end
|
27
|
+
|
28
|
+
def see_html_task_output_message
|
29
|
+
@html_output_filename = "#{APP_ROOT}/test/tasks_test/doc/behaviors.html"
|
30
|
+
assert_match /Wrote #{@html_output_filename}/, @report
|
31
|
+
end
|
32
|
+
|
33
|
+
def see_that_html_report_file_exits
|
34
|
+
assert File.exists?(@html_output_filename), "html output file should exist"
|
35
|
+
end
|
36
|
+
|
37
|
+
def html_report_file_should_contain(user_behaviors)
|
38
|
+
file_contents = File.read(@html_output_filename)
|
39
|
+
user_behaviors.each do |line|
|
40
|
+
assert_match /#{line}/, file_contents
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# TESTS
|
46
|
+
#
|
47
|
+
def test_that_behaviors_tasks_should_list_behavioral_definitions_for_the_classes_under_test
|
48
|
+
run_behaviors_task
|
49
|
+
user_behaviors = [
|
50
|
+
"User should:",
|
51
|
+
" - be able set user name and age during construction",
|
52
|
+
" - be able to get user name and age",
|
53
|
+
" - be able to ask if a user is an adult"
|
54
|
+
]
|
55
|
+
assert_match /#{user_behaviors.join("\n")}/, @report
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_that_behaviors_tasks_should_list_behavioral_definitions_for_the_classes_under_test_in_html_output
|
59
|
+
run_behaviors_html_task
|
60
|
+
see_html_task_output_message
|
61
|
+
see_that_html_report_file_exits
|
62
|
+
user_behaviors = [
|
63
|
+
"User should:",
|
64
|
+
"be able set user name and age during construction",
|
65
|
+
"be able to get user name and age",
|
66
|
+
"be able to ask if a user is an adult"
|
67
|
+
]
|
68
|
+
html_report_file_should_contain user_behaviors
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.expand_path(File.dirname(__FILE__)) + '/../lib/behaviors'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
loading_developer_test_class_stdout = StringIO.new
|
6
|
+
saved_stdout = $stdout.dup
|
7
|
+
$stdout = loading_developer_test_class_stdout
|
8
|
+
|
9
|
+
class DeveloperTest
|
10
|
+
extend Behaviors
|
11
|
+
attr_accessor :flunk_msg, :tested_code
|
12
|
+
|
13
|
+
should "test their code" do
|
14
|
+
@tested_code = true
|
15
|
+
end
|
16
|
+
should "go to meetings"
|
17
|
+
end
|
18
|
+
|
19
|
+
$stdout = saved_stdout
|
20
|
+
loading_developer_test_class_stdout.rewind
|
21
|
+
$loading_developer_test_class_output = loading_developer_test_class_stdout.read
|
22
|
+
|
23
|
+
class BehaviorsTest < Test::Unit::TestCase
|
24
|
+
|
25
|
+
|
26
|
+
def setup
|
27
|
+
@target = DeveloperTest.new
|
28
|
+
assert_nil @target.tested_code, "block called too early"
|
29
|
+
end
|
30
|
+
|
31
|
+
#
|
32
|
+
# TESTS
|
33
|
+
#
|
34
|
+
def test_should_called_with_a_block_defines_a_test
|
35
|
+
assert @target.methods.include?("test_should_test their code"), "Missing test method"
|
36
|
+
|
37
|
+
@target.send("test_should_test their code")
|
38
|
+
|
39
|
+
assert @target.tested_code, "block not called"
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_called_without_a_block_does_not_create_a_test_method
|
43
|
+
assert !@target.methods.include?("test_should_go to meetings"), "Should not have method"
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_should_called_without_a_block_will_give_unimplemented_output_when_class_loads
|
47
|
+
unimplemented_output = "UNIMPLEMENTED CASE: Developer should go to meetings"
|
48
|
+
assert_match /#{unimplemented_output}/, $loading_developer_test_class_output
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
APP_ROOT = File.expand_path(File.dirname(__FILE__))
|
5
|
+
load "#{APP_ROOT}/../../tasks/behaviors_tasks.rake"
|
6
|
+
|
7
|
+
cd APP_ROOT
|
8
|
+
|
9
|
+
desc 'Default: run unit tests.'
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
Rake::TestTask.new(:test) do |t|
|
13
|
+
t.libs << "#{APP_ROOT}/../../lib"
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = true
|
16
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<style>
|
4
|
+
|
5
|
+
div.title
|
6
|
+
{
|
7
|
+
width: 600px;
|
8
|
+
font: bold 14pt trebuchet ms;
|
9
|
+
}
|
10
|
+
|
11
|
+
div.specification
|
12
|
+
{
|
13
|
+
font: bold 12pt trebuchet ms;
|
14
|
+
border: solid 1px black;
|
15
|
+
width: 600px;
|
16
|
+
padding: 5px;
|
17
|
+
margin: 5px;
|
18
|
+
}
|
19
|
+
|
20
|
+
ul.requirements
|
21
|
+
{
|
22
|
+
font: normal 11pt verdana;
|
23
|
+
padding-left: 0;
|
24
|
+
margin-left: 0;
|
25
|
+
border-bottom: 1px solid gray;
|
26
|
+
width: 600px;
|
27
|
+
}
|
28
|
+
|
29
|
+
ul.requirements li
|
30
|
+
{
|
31
|
+
list-style: none;
|
32
|
+
margin: 0;
|
33
|
+
padding: 0.25em;
|
34
|
+
border-top: 1px solid gray;
|
35
|
+
}
|
36
|
+
</style>
|
37
|
+
</head>
|
38
|
+
<body>
|
39
|
+
<div class="title">Specifications for tasks_test</div>
|
40
|
+
|
41
|
+
<div class="specification">
|
42
|
+
User should:
|
43
|
+
<ul class="requirements">
|
44
|
+
|
45
|
+
<li>be able set user name and age during construction</li>
|
46
|
+
|
47
|
+
<li>be able to get user name and age</li>
|
48
|
+
|
49
|
+
<li>be able to ask if a user is an adult</li>
|
50
|
+
|
51
|
+
</ul>
|
52
|
+
</div>
|
53
|
+
|
54
|
+
</body>
|
55
|
+
</html>
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'behaviors'
|
3
|
+
|
4
|
+
require 'user'
|
5
|
+
|
6
|
+
class UserTest < Test::Unit::TestCase
|
7
|
+
extend Behaviors
|
8
|
+
|
9
|
+
def setup
|
10
|
+
end
|
11
|
+
|
12
|
+
should "be able set user name and age during construction"
|
13
|
+
should "be able to get user name and age"
|
14
|
+
should "be able to ask if a user is an adult"
|
15
|
+
def test_DELETEME
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: behaviors
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-10-19 00:00:00 -04:00
|
8
|
+
summary: behavior-driven unit test helper
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: dev@atomicobject.com
|
12
|
+
homepage: http://atomicobject.com
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Atomic Object LLC
|
30
|
+
files:
|
31
|
+
- test/behaviors_tasks_test.rb
|
32
|
+
- test/behaviors_test.rb
|
33
|
+
- test/tasks_test
|
34
|
+
- test/tasks_test/doc
|
35
|
+
- test/tasks_test/lib
|
36
|
+
- test/tasks_test/Rakefile
|
37
|
+
- test/tasks_test/test
|
38
|
+
- test/tasks_test/doc/behaviors.html
|
39
|
+
- test/tasks_test/lib/user.rb
|
40
|
+
- test/tasks_test/test/user_test.rb
|
41
|
+
- lib/behaviors.rb
|
42
|
+
test_files: []
|
43
|
+
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
extra_rdoc_files: []
|
47
|
+
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
dependencies: []
|
55
|
+
|