jtrupiano (owner)

Revisions

gist: 106484 Download_button fork
public
Public Clone URL: git://gist.github.com/106484.git
Text only
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
# Custom shoulda macro example -- validating POST data in a controller
 
# The macro -- note that it references a create_user() function. This function is included beneath the macro
 
def should_require(att)
  should "require #{att}" do
    create_user(att => nil)
    assert assigns(:user).errors.on(att)
    assert_response :success
    assert_template 'new'
  end
end
 
  protected
    def create_user(options = {})
      post :create, :user => user_attrs.merge(options)
    end
 
    def user_attrs
      { :email => 'quire@example.com', :password => 'quire69', :password_confirmation => 'quire69', :name => 'Quire' }
    end
 
# Now, to use it in a sample nested context:
 
class UsersControllerTest < ActionController::TestCase
  context "A user is ready to submit a form to create an account for an email that doesn't yet exist" do
    should_require :email
    should_require :password
    should_require :password_confirmation
    should_require :name
  end
end