Guest User

Untitled

a guest
Mar 6th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. # Custom shoulda macro example -- validating POST data in a controller
  2.  
  3. # The macro -- note that it references a create_user() function. This function is included beneath the macro
  4.  
  5. def should_require(att)
  6. should "require #{att}" do
  7. create_user(att => nil)
  8. assert assigns(:user).errors.on(att)
  9. assert_response :success
  10. assert_template 'new'
  11. end
  12. end
  13.  
  14. protected
  15. def create_user(options = {})
  16. post :create, :user => user_attrs.merge(options)
  17. end
  18.  
  19. def user_attrs
  20. { :email => 'quire@example.com', :password => 'quire69', :password_confirmation => 'quire69', :name => 'Quire' }
  21. end
  22.  
  23. # Now, to use it in a sample nested context:
  24.  
  25. class UsersControllerTest < ActionController::TestCase
  26. context "A user is ready to submit a form to create an account for an email that doesn't yet exist" do
  27. should_require :email
  28. should_require :password
  29. should_require :password_confirmation
  30. should_require :name
  31. end
  32. end
Add Comment
Please, Sign In to add comment