Could you tell me how to test regex-code correctly?
I test my user-login attribute with following code:
# user.rb
class User < ActiveRecord::Base
#...
validates_format_of :login, :with => /^[a-zA-z0-9_.]{3,18}$/
end
# user_spec.rb
describe User do
before(:each) do
@user = Factory.build(:user)
@user.save
end
subject { @user }
it { should be_valid }
it { should_not allow_value("b lah").for(:login) }
it { should_not allow_value("bälah").for(:login) }
it { should_not allow_value("b@lah").for(:login) }
it { should_not allow_value("bülah").for(:login) }
it { should_not allow_value("bßlah").for(:login) }
it { should_not allow_value("b!lah").for(:login) }
it { should_not allow_value("b%lah").for(:login) }
it { should_not allow_value("b)lah").for(:login) }
# ....
# Shall I test here every special sign????
end
But it seems very redundant and not secure.... Is there a best practice? Thx!