Skip to content

Commit

Permalink
Implemented User Model validation and tested in user_spec.rb, the tes…
Browse files Browse the repository at this point in the history
…ts using users factory
  • Loading branch information
Kornelije Sajler committed Nov 5, 2012
1 parent 2c239f5 commit f2edd55
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ class User < ActiveRecord::Base
has_secure_password

attr_accessible :email, :password, :password_confirmation, :full_name
attr_readonly :email

validates :email, presence: true, uniqueness: true,
format: { with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/,
message: 'The format of Email is invalid'}
validates :password, presence: true, length: { minimum: 8 }
validates :full_name, presence: true
end
8 changes: 4 additions & 4 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :user do
email "MyString"
password_digest "MyString"
email 'xajler@gmail.com'
password 'x1234567'
password_digest 'x1234567'
full_name 'Kornelije Sajler'
end
end
54 changes: 53 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
require 'spec_helper'

describe User do
pending "add some examples to (or delete) #{__FILE__}"
let :user do
build :user
end

subject do
user
end

context 'is invalid' do
it 'when required #email is not given' do
user.email = ''
should_not be_valid
end

it 'when required #password is not given' do
user.password = ''
should_not be_valid
end

it 'when required #full_name is not given' do
user.full_name = ''
should_not be_valid
end

it 'when #email is not unique' do
user.save
user1 = build :user
user1.save

user1.should_not be_valid
user1.errors.full_messages[0].should match 'Email has already been taken'
end

it 'when #email format is not valid' do
user.email = 'invalid mail'
should_not be_valid
end

it 'when #password is not at least 8 characters' do
user.password = 'abc123'
should_not be_valid
end
end

it '#email must not ever change after it is created' do
user.save
user.update_attributes email: 'ksajler@gmail.com'
user.reload.email.should match 'xajler@gmail.com'
end

it 'is valid' do
should be_valid
end
end

0 comments on commit f2edd55

Please sign in to comment.