Skip to content

Instantly share code, notes, and snippets.

@dnch
Forked from jimsynz/user.rb
Created December 19, 2011 22:50
Show Gist options
  • Save dnch/1499277 to your computer and use it in GitHub Desktop.
Save dnch/1499277 to your computer and use it in GitHub Desktop.
WTF!
class User
include Mongoid::Document
field :email
validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i, :message => "is not a valid email adress."
end
# Maybe I'm just old-school, but I prefer this...
describe User do
it "validates email addresses" do
subject.email = 'bot@kimonoapp.com'
subject.should be_valid
subject.email = 'b!o^t@kimonoapp.com'
subject.should_not be_valid
# don't forget to check for valid and invalid domains...
subject.email = 'bot@kimono-app.com'
subject.should be_valid
subject.email = 'bot@kimono_app.com'
subject.should_not be_valid
# and those bastards, leading and trailing whitespace...
subject.email = ' bot@kimonoapp.com'
subject.should_not be_valid
subject.email = 'bot@kimonoapp.com '
subject.should_not be_valid
end
3) User
Failure/Error: it { should validate_format_of(:email).to_allow('bot@kimonoapp.com').not_to_allow('b!o^t@kimonoapp.com') }
Expected User to validate format of "email" allowing the value "bot@kimonoapp.com" and not allowing the value "b!o^t@kimonoapp.com"; instead got "format" validator on "email" with "b!o^t@kimonoapp.com" as a valid value
# ./spec/models/user_spec.rb:15:in `__script__'
# kernel/common/eval18.rb:43:in `instance_eval'
# kernel/bootstrap/array18.rb:16:in `map'
# kernel/bootstrap/array18.rb:16:in `map'
# kernel/loader.rb:686:in `run_at_exits'
# kernel/loader.rb:706:in `epilogue'
# kernel/loader.rb:837:in `main'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment