public
Fork of pelargir/test_spec_on_rails
Description: Rails plugin with helpers to test your app using test/spec.
Homepage: http://agilewebdevelopment.com/plugins/test_spec_on_rails
Clone URL: git://github.com/defunkt/test_spec_on_rails.git
Search Repo:
Matthew Bass (author)
Wed Apr 02 18:40:14 -0700 2008
commit  0e4a4ed4cbbb70fbf7a274c8890f2d0836ea6faf
tree    9302f397f8b539cb3d4c03b471efe27a1be7a59f
100644 156 lines (104 sloc) 4.094 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
test/spec/rails by Per "Tuxie" Wigren <per.wigren@gmail.com>
 
This plugin contain some helpers to test your Rails app using test/spec.
 
Installation
------------
 * Install test/spec: gem install test-spec
 * Install test/spec/rails
 * require 'test/spec/rails' in your test_helper.rb
 
 
 
 
Model validation
----------------
  @user.should.validate
  @user.should.not.validate
  or:
  @user.should.be.validated
  @user.should.not.be.validated
 
 
Redirection
-----------
  response.should.be.redirected # assert_response :redirect
  response.should.redirect foo_url(@foo) # assert_redirected_to foo_url(@foo)
  should.redirect_to :action => 'show' # because "response" is optional
  
  It's aliased as redirect, redirect_to, redirected and redirected_to
 
 
Output verification
-------------------
  Wrapper for assert_select:
  
  get :show
  page.should.select "form#user_form" # require the output to have a <form id="user_form">
  
  page.should.select "form#user_form" do |form|
    form.should.select "input[type=submit]" # the user_form must include a <input type="submit">
  end
 
 
HTTP Status
-----------
  Wrapper for assert_response:
 
  status.should.be :success
  status.should.be 200
 
 
Which template was rendered?
----------------------------
  Wrapper for assert_template:
  
  template.should.be "foo/show.rhtml"
 
 
Which layout used?
------------------
  Wrapper for asserting that a certain layout is used:
  
  layout.should.be "foo"
 
 
URL testing
-----------
  get :show, :user_id => 1
  url.should.be "/users/1"
  url.should.be :controller => "users", :action => "show", :user_id => 1
      
 
Difference
----------
  Wrapper for assert_difference:
  
  Article.should.differ(:count).by(2) { blah }
 
 
Routing to set of url options from a given path (i.e., #assert_generates)
-------------------------------------------------------------------------
 
  assert_generates "/users/1", :controller => "users", :action => "show", :id => "1"
    
    becomes
 
  {:controller => "users", :action => "show", :id => "1"}.should.route_to "/users/1"
 
 
Routing from a set of url options to an expected path (i.e., #assert_recognizes)
--------------------------------------------------------------------------------
 
  assert_recognizes({:controller => "users", :action => "show", :id => "1"}, {:path => "/users/1", :method => :get})
 
    becomes
 
  {:path => "/users/1", :method => :get}.should.route_from :controller => "users", :action => "show", :id =>"1"
 
 
 
Setup controller for testing + example usage
--------------------------------------------
  context "A guest" do
    use_controller FooController
    
    specify "isn't allowed to foo" do
      post :create, :foo => 'bar'
      flash[:error].should.not.be.blank
      response.should.redirect :controller => 'front', :action => 'show'
      assigns(:foo).errors.on(:bar).should.not.be.blank
      assigns(:foo).should.not.validate
      follow_redirect
      page.should.select "#errors", :text => /#{flash[:error]}/
    end
  end
  
  context "Tuxie" do
    setup do
      use_controller FooController
      login_as :tuxie
    end
    
    specify "may foo" do
      post :create, :foo => 'bar'
      flash[:notice].should.not.be.blank
      assigns(:foo).errors.should.be.empty
      assigns(:foo).should.validate
      should.redirect_to :action => 'show'
      follow_redirect
      page.should.select "#notice", :text => /#{flash[:notice]}/
    end
  end
 
 
Misc
----
 
  page, response and request are just aliases for self (the TestCase) so the following
  are functionally identical:
  
  page.should.redirect
  response.should.redirect
  request.should.redirect
  
  output, body, html and xhtml return @response.body so the following lines are identical:
  
  output.should.match /foo/
  html.should.match /foo/
  xhtml.should.match /foo/
  body.should.match /foo/
  
  If an object respond to #should_equal, that method will be used instead of assert_equal
  when using foo.should.equal(bar) or foo.should.be(bar)
  foo.should.not.equal and foo.should.not.be look for #should_not_equal