Extract the validation of Username format in own class to avoid

complexity in user model object
This commit is contained in:
Cyril Mougel
2013-02-08 15:52:56 +01:00
committed by Neil Lalonde
parent 3ccfa645a8
commit 84191802df
5 changed files with 81 additions and 35 deletions

View File

@@ -193,7 +193,6 @@ describe User do
end
end
describe 'new' do
subject { Fabricate.build(:user) }
@@ -394,20 +393,6 @@ describe User do
end
end
context '.username_valid?' do
it 'returns true when username is both valid and available' do
User.username_valid?('Available').should be_true
end
it 'returns true when the username is valid but not available' do
User.username_valid?(Fabricate(:user).username).should be_true
end
it 'returns false when the username is not valid' do
User.username_valid?('not valid.name').should be_false
end
end
describe '.suggest_username' do
it 'corrects weird characters' do
User.suggest_username("Darth%^Vadar").should == "Darth_Vadar"

View File

@@ -0,0 +1,17 @@
require 'spec_helper'
describe UsernameValidator do
context "#valid_format?" do
it 'returns true when username is both valid and available' do
expect(UsernameValidator.new('Available').valid_format?).to eq true
end
it 'returns true when the username is valid but not available' do
expect(UsernameValidator.new(Fabricate(:user).username).valid_format?).to eq true
end
it 'returns false when the username is not valid' do
expect(UsernameValidator.new('not valid.name').valid_format?).to eq false
end
end
end