Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment