Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 133 lines (93 sloc) 4.44 kb
e9eba10 documentation and fixes to :through test
tsaleh authored
1 = Shoulda - Making tests easy on the fingers and eyes
9bf2ba7 documentation
tsaleh authored
2
0ea1fc7 Tons of documentation
tsaleh authored
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.
9bf2ba7 documentation
tsaleh authored
4
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
5 Helpers:: #context and #should give you rSpec like test blocks.
0ea1fc7 Tons of documentation
tsaleh authored
6 In addition, you get nested contexts and a much more readable syntax.
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
7 Macros:: Generate hundreds of lines of Controller and ActiveRecord tests with these powerful macros.
0ea1fc7 Tons of documentation
tsaleh authored
8 They get you started quickly, and can help you ensure that your application is conforming to best practices.
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
9 Assertions:: Many common rails testing idioms have been distilled into a set of useful assertions.
9bf2ba7 documentation
tsaleh authored
10
0ea1fc7 Tons of documentation
tsaleh authored
11 = Usage
12
b415bff Revert bunch of bad commits:
Tammer Saleh authored
13 === Context Helpers (ThoughtBot::Shoulda::Context)
0ea1fc7 Tons of documentation
tsaleh authored
14
15 Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
16
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
17 class UserTest << Test::Unit::TestCase
0ea1fc7 Tons of documentation
tsaleh authored
18 context "A User instance" do
19 setup do
20 @user = User.find(:first)
21 end
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
22
915f76b typos
tsaleh authored
23 should "return its full name" do
0ea1fc7 Tons of documentation
tsaleh authored
24 assert_equal 'John Doe', @user.full_name
25 end
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
26
0ea1fc7 Tons of documentation
tsaleh authored
27 context "with a profile" do
28 setup do
29 @user.profile = Profile.find(:first)
30 end
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
31
915f76b typos
tsaleh authored
32 should "return true when sent #has_profile?" do
0ea1fc7 Tons of documentation
tsaleh authored
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
84ea946 @rmm5t Reorganized ActiveRecord macros and assertions
rmm5t authored
46 === ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord::Macros)
0ea1fc7 Tons of documentation
tsaleh authored
47
48 Quick macro tests for your ActiveRecord associations and validations:
49
50 class PostTest < Test::Unit::TestCase
42d4de6 @rmm5t Deprecated load_all_fixtures [#65]
rmm5t authored
51 fixtures :all
0ea1fc7 Tons of documentation
tsaleh authored
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 should_have_many :posts
64
65 should_not_allow_values_for :email, "blah", "b lah"
66 should_allow_values_for :email, "a@b.com", "asdf@asdf.com"
67 should_ensure_length_in_range :email, 1..100
68 should_ensure_value_in_range :age, 1..100
69 should_protect_attributes :password
70 end
71
72 Makes TDD so much easier.
73
6bc83d7 @rmm5t Doc updates
rmm5t authored
74 === Controller Tests (ThoughtBot::Shoulda::Controller::Macros)
0ea1fc7 Tons of documentation
tsaleh authored
75
76 Macros to test the most common controller patterns...
77
78 context "on GET to :show for first record" do
79 setup do
80 get :show, :id => 1
81 end
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
82
0ea1fc7 Tons of documentation
tsaleh authored
83 should_assign_to :user
84 should_respond_with :success
85 should_render_template :show
86 should_not_set_the_flash
87
88 should "do something else really cool" do
89 assert_equal 1, assigns(:user).id
90 end
91 end
92
93 Test entire controllers in a few lines...
94
95 class PostsControllerTest < Test::Unit::TestCase
96 should_be_restful do |resource|
97 resource.parent = :user
98
99 resource.create.params = { :title => "first post", :body => 'blah blah blah'}
100 resource.update.params = { :title => "changed" }
101 end
102 end
103
104 should_be_restful generates 40 tests on the fly, for both html and xml requests.
105
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
106 === Helpful Assertions (ThoughtBot::Shoulda::Assertions)
0ea1fc7 Tons of documentation
tsaleh authored
107
108 More to come here, but have fun with what's there.
109
110 assert_same_elements([:a, :b, :c], [:c, :a, :b])
111 assert_contains(['a', '1'], /\d/)
112 assert_contains(['a', '1'], 'a')
113
56a3f43 Added shoulda_macros directory support as per http://thoughtbot.light…
Tammer Saleh authored
114 === 3rd Party and Application Specific Macros
115
116 Any *.rb file under RAILS_ROOT/test/shoulda_macros/ or vendor/(plugins|gems)/gem_name/shoulda_macros/ will be automatically required when you run your tests. This allows you to distribute macros with your plugins, or to organize the macros inside your application. Remember to add your macro to Test::Unit::TestCase in the macro file:
117
118 # test/shoulda_macros/security.rb
119 class Test::Unit::TestCase
120 def self.should_be_denied(opts = {})
121 should_set_the_flash_to(opts[:flash] || /Please log in/i)
122 should_redirect_to(opts[:redirect] || 'login_url')
123 end
124 end
125
0ea1fc7 Tons of documentation
tsaleh authored
126 = Credits
127
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
128 Shoulda is maintained by {Tammer Saleh}[mailto:tsaleh@thoughtbot.com], and is funded by Thoughtbot[http://www.thoughtbot.com], inc.
0ea1fc7 Tons of documentation
tsaleh authored
129
130 = License
9bf2ba7 documentation
tsaleh authored
131
56a3f43 Added shoulda_macros directory support as per http://thoughtbot.light…
Tammer Saleh authored
132 Shoulda is Copyright © 2006-2008 Tammer Saleh, Thoughtbot. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
Something went wrong with that request. Please try again.