Skip to content

Instantly share code, notes, and snippets.

@Zequez
Created September 1, 2011 11:19
Show Gist options
  • Save Zequez/1185979 to your computer and use it in GitHub Desktop.
Save Zequez/1185979 to your computer and use it in GitHub Desktop.
My tests are a mess
require 'spec_helper'
describe User do
subject { @user }
before(:each) { @user = User.new }
context "when creating .new User" do
before(:each) { @user = User.new }
context "with valid data" do
before(:each) { @user = Factory.build :user }
it 'should be valid' do
should be_valid
end
it "shouldn't have any error" do
should have(0).errors
end
end
context "with invalid data" do
before(:each) { @user = Factory.build :user }
context "#name" do
it 'should not allow with weird symbols' do
should_not allow_value('Zeq$uez').for :name
should_not allow_value('Zeq%uez').for :name
should_not allow_value('Zeq uez').for :name
should_not allow_value('Zeq#uez').for :name
end
it 'should not allow too large' do
should_not allow_value('Z'*65).for :name
end
it 'should not allow too short' do
should_not allow_value('ZZ').for :name
end
it 'should not allow duplicated' do
Factory :user, :name => 'Zequez'
should_not allow_value('Zequez').for :name
end
it 'should not allow blank' do
should_not allow_value('').for :name
end
end
context '#email' do
it 'should not allow invalid emails' do
should_not allow_value('zequez @gmail.com').for :email
should_not allow_value('zequez@@gmail.com').for :email
should_not allow_value('zequez@gmailcommm').for :email
should_not allow_value('zequez@g_mail.com').for :email
should_not allow_value('.zequez@gmail.com').for :email
should_not allow_value('_zequez@gmail.com').for :email
should_not allow_value('-zequez@gmail.com').for :email
end
it 'should not allow duplicated' do
Factory :user, :email => 'zequez@gmail.com'
@user = User.new
should_not allow_value('zequez@gmail.com').for :email
end
it 'should not allow too large' do
should_not allow_value('zequez@gmail.com' + 'm'*128).for :email
end
it 'should not allow blank' do
should_not allow_value('').for :email
end
end
context '#password' do
it 'should not allow less than 6 characters' do
@user.password_confirmation = 'asdas'
should_not allow_value('asdas').for :password
end
it 'should not allow if not confirmed' do
should_not allow_value('aDMJI"lkKASID#)="USDA').for :password
end
it 'should now allow a blank password' do
@user.password_confirmation = ''
should_not allow_value('').for :password
end
end
end
context 'with bulk assignation' do
it 'should not allow bulk assign the #name' do
@user.attributes = {:name => 'Zequez'}
@user.name.should_not eq 'Zequez'
end
it 'should not allow bulk assign the #email' do
@user.attributes = {:email => 'zequez@gmail.com'}
@user.email.should_not eq 'zequez@gmail.com'
end
it 'should not allow bulk assign the #role' do
@user.attributes = {:role => :admin}
@user.role.should_not eq :admin
end
it 'should allow bulk assign #password' do
@user.attributes = {:password => 'holahola'}
@user.password.should eq 'holahola'
end
it 'should allow bulk assign #password_confirmation' do
@user.attributes = {:password_confirmation => 'holahola'}
@user.password_confirmation.should eq 'holahola'
end
end
end
context 'when saving an existant user' do
before :each do
@user = Factory :user
end
context :pass do
it 'should allow to save with a blank password' do
@user.password = @user.password_confirmation = nil
end
end
end
it '#role? should return true if the user is from the given role' do
@user.role = :admin
@user.role?(:admin).should eq true
end
it '#role? should return true if the user is from at least one of the given roles' do
@user.role = :mod
@user.role?(:admin, :mod, :user).should eq true
end
it "#user? should return true if it's of the :user role" do
@user.role = :user
@user.user?.should eq true
end
it "#mod? should return true if it's of the :mod role" do
@user.role = :mod
@user.mod?.should eq true
end
it "#admin? should return true if it's of the :admin role" do
@user.role = :admin
@user.admin?.should eq true
end
it "#superadmin? should return true if it's of the :superadmin role" do
@user.role = :superadmin
@user.superadmin?.should eq true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment