Skip to content

Instantly share code, notes, and snippets.

@jtrupiano
Created May 4, 2009 14:27
Show Gist options
  • Save jtrupiano/106484 to your computer and use it in GitHub Desktop.
Save jtrupiano/106484 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment