thechrisoshow / shoulda forked from thoughtbot/shoulda

Makes tests easy on the fingers and the eyes

shoulda / README.rdoc
e9eba101 » tsaleh 2007-08-07 documentation and fixes to ... 1 = Shoulda - Making tests easy on the fingers and eyes
9bf2ba7a » tsaleh 2007-04-05 documentation 2
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 3 Shoulda makes it easy to write elegant, understandable, and maintainable tests. Shoulda consists of test macros, assertions, and helpers added on to the Test::Unit framework. It's fully compatible with your existing tests, and requires no retooling to use.
9bf2ba7a » tsaleh 2007-04-05 documentation 4
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 5 Helpers:: #context and #should give you rSpec like test blocks.
6 In addition, you get nested contexts and a much more readable syntax.
7 Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
8 They get you started quickly, and can help you ensure that your application is conforming to best practices.
9 Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
9bf2ba7a » tsaleh 2007-04-05 documentation 10
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 11 = Usage
12
13 === Context Helpers (ThoughtBot::Shoulda::Context)
14
15 Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
16
5caf0420 » tsaleh 2007-08-27 documentation fixes 17 class UserTest << Test::Unit::TestCase
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 18 context "A User instance" do
19 setup do
20 @user = User.find(:first)
21 end
22
915f76bc » tsaleh 2008-04-05 typos 23 should "return its full name" do
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 24 assert_equal 'John Doe', @user.full_name
25 end
26
27 context "with a profile" do
28 setup do
29 @user.profile = Profile.find(:first)
30 end
31
915f76bc » tsaleh 2008-04-05 typos 32 should "return true when sent #has_profile?" do
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 33 assert @user.has_profile?
34 end
35 end
36 end
37 end
38
39 Produces the following test methods:
40
41 "test: A User instance should return its full name."
42 "test: A User instance with a profile should return true when sent #has_profile?."
43
44 So readable!
45
46 === ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord)
47
48 Quick macro tests for your ActiveRecord associations and validations:
49
50 class PostTest < Test::Unit::TestCase
51 load_all_fixtures
52
53 should_belong_to :user
54 should_have_many :tags, :through => :taggings
55
56 should_require_unique_attributes :title
57 should_require_attributes :body, :message => /wtf/
58 should_require_attributes :title
59 should_only_allow_numeric_values_for :user_id
60 end
61
62 class UserTest < Test::Unit::TestCase
63 load_all_fixtures
64
65 should_have_many :posts
66
67 should_not_allow_values_for :email, "blah", "b lah"
68 should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
69 should_ensure_length_in_range :email, 1..100
70 should_ensure_value_in_range :age, 1..100
71 should_protect_attributes :password
72 end
73
74 Makes TDD so much easier.
75
76 === Controller Tests (ThoughtBot::Shoulda::Controller::ClassMethods)
77
78 Macros to test the most common controller patterns...
79
80 context "on GET to :show for first record" do
81 setup do
82 get :show, :id => 1
83 end
1ecd0291 » tsaleh 2007-04-05 - documentation 84
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 85 should_assign_to :user
86 should_respond_with :success
87 should_render_template :show
88 should_not_set_the_flash
89
90 should "do something else really cool" do
91 assert_equal 1, assigns(:user).id
92 end
93 end
94
95 Test entire controllers in a few lines...
96
97 class PostsControllerTest < Test::Unit::TestCase
98 should_be_restful do |resource|
99 resource.parent = :user
100
101 resource.create.params = { :title => "first post", :body => 'blah blah blah'}
102 resource.update.params = { :title => "changed" }
103 end
104 end
105
106 should_be_restful generates 40 tests on the fly, for both html and xml requests.
107
108 === Helpful Assertions (ThoughtBot::Shoulda::General)
109
110 More to come here, but have fun with what's there.
111
112 load_all_fixtures
113 assert_same_elements([:a, :b, :c], [:c, :a, :b])
114 assert_contains(['a', '1'], /\d/)
115 assert_contains(['a', '1'], 'a')
116
117 = Credits
118
551f666c » tsaleh 2008-04-21 Just testing out github. 119 Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
0ea1fc75 » tsaleh 2007-07-24 Tons of documentation 120
121 = License
9bf2ba7a » tsaleh 2007-04-05 documentation 122
fb26b71f » tsaleh 2007-11-26 converted to the MIT license 123 Shoulda is Copyright © 2006-2007 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.