I'm testing the validation of emails so I did the following:
it 'should only allow domains names with numbers, letters, dots or
dashes' do
(%-\\|!"#$\%&/()=?¡;,:{}+'<>°+*"¬~^@áéíóú´`_ -).each_char do
c>
Factory.build(:user, :email =>
"zequez@gm#{c}ail.com").should_not be_valid
end
end
Also this:
['1@1.com',
'1-a-1@ar.com',
'zequez..-@com.com',
'z_e_q_u_e_z@t-p.9.asd.g.r.t.a.c.com',
'zambuche@t-p.9.asd.g.r.t.a.c.com',
'zEzEqZ@CoM.Ar',
'zzzzzzzzzzz@asd.com',
'z@q.z',
'yo@zequez.com',
'ca..--__ca..--__ca..--__a@zequez.com'].each do |email|
it "should allow #{email} as a valid email" do
Factory.build(:user, :email => email).should be_valid
end
end
'1-a-1@ar.com',
'zequez..-@com.com',
'z_e_q_u_e_z@t-p.9.asd.g.r.t.a.c.com',
'zambuche@t-p.9.asd.g.r.t.a.c.com',
'zEzEqZ@CoM.Ar',
'zzzzzzzzzzz@asd.com',
'z@q.z',
'yo@zequez.com',
'ca..--__ca..--__ca..--__a@zequez.com'].each do |email|
it "should allow #{email} as a valid email" do
Factory.build(:user, :email => email).should be_valid
end
end
And many similar things…
Is that bad?
In my book this is fine… you have a list of specific examples and it is clear what you are doing. I might go further and make sure that in fact it is the email which is valid/invalid vs just checking the model (at least in the case if you are checking what should be invalid data) as another field could be the invalid one.
I also like shoulda as you get helpers like:
it { should allow_value(‘david.n_kahn@gmail.com.mx’).for(:email) }
it { should_not allow_value(‘david nathan kahn at gmail dot com’).for(:email) }
On Sun, Aug 28, 2011 at 10:29 AM, Ezequiel Schwartzman
<lists@ruby-forum.com
In my book this is fine.... you have a list of specific examples and it
is
clear what you are doing. I might go further and make sure that in fact
it
is the email which is valid/invalid vs just checking the model (at least
in
the case if you are checking what should be invalid data) as another
field
could be the invalid one.
I also like shoulda as you get helpers like:
it { should allow_value('david.n_kahn@gmail.com.mx').for(:email) }
it { should_not allow_value('david nathan kahn at gmail dot
com').for(:email) }
Thanks! I'm looking into shoulda now, it seems very useful! ^^