Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 135 lines (94 sloc) 4.467 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
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
6bc83d7 @rmm5t Doc updates
rmm5t authored
76 === Controller Tests (ThoughtBot::Shoulda::Controller::Macros)
0ea1fc7 Tons of documentation
tsaleh authored
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
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
84
0ea1fc7 Tons of documentation
tsaleh authored
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
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
108 === Helpful Assertions (ThoughtBot::Shoulda::Assertions)
0ea1fc7 Tons of documentation
tsaleh authored
109
110 More to come here, but have fun with what's there.
111
112 assert_same_elements([:a, :b, :c], [:c, :a, :b])
113 assert_contains(['a', '1'], /\d/)
114 assert_contains(['a', '1'], 'a')
115
56a3f43 Added shoulda_macros directory support as per http://thoughtbot.lighthou...
Tammer Saleh authored
116 === 3rd Party and Application Specific Macros
117
118 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:
119
120 # test/shoulda_macros/security.rb
121 class Test::Unit::TestCase
122 def self.should_be_denied(opts = {})
123 should_set_the_flash_to(opts[:flash] || /Please log in/i)
124 should_redirect_to(opts[:redirect] || 'login_url')
125 end
126 end
127
0ea1fc7 Tons of documentation
tsaleh authored
128 = Credits
129
526b123 @rmm5t Added shoulda/assertions and moved generic assertions into it
rmm5t authored
130 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
131
132 = License
9bf2ba7 documentation
tsaleh authored
133
56a3f43 Added shoulda_macros directory support as per http://thoughtbot.lighthou...
Tammer Saleh authored
134 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.