Skip to content

Instantly share code, notes, and snippets.

@boyvanamstel
Created July 7, 2011 09:57
Show Gist options
  • Save boyvanamstel/1069215 to your computer and use it in GitHub Desktop.
Save boyvanamstel/1069215 to your computer and use it in GitHub Desktop.
Test::Unit test for valid url and implementation of valid url testing in Rails
# Test in test/functional/sites_controller_test.rb
test "should contain a valid url" do
@site.url = "random text"
assert @site.invalid?
end
# Implementation in app/models/site.rb
class Site < ActiveRecord::Base
validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment