Permalink
Browse files

Implemented User Model validation and tested in user_spec.rb, the tes…

…ts using users factory
  • Loading branch information...
1 parent 2c239f5 commit f2edd5551c915bba59b3aabb8c894325cf4f5414 @xajler committed Nov 5, 2012
Showing with 64 additions and 5 deletions.
  1. +7 −0 app/models/user.rb
  2. +4 −4 spec/factories/users.rb
  3. +53 −1 spec/models/user_spec.rb
View
@@ -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
@@ -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
@@ -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.