添加关于User model的测试

This commit is contained in:
ccx1024cc 2016-12-11 15:56:58 +08:00
parent 83343b9eb1
commit 38a754ee3c
4 changed files with 107 additions and 1 deletions

View File

@ -24,7 +24,7 @@ gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'

View File

@ -3,4 +3,11 @@ class User < ActiveRecord::Base
has_and_belongs_to_many :missions
has_many :notes
has_secure_password # 等价于验证password_confirm和password是否相等并且验证password是否存在
validates :name, presence: true, uniqueness: true, length: { maximum: 20 }
validates :email, presence: true, uniqueness: true, length: { maximum: 50},
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
validates :password, length: { minimum: 6 }
end

25
spec/factories/users.rb Normal file
View File

@ -0,0 +1,25 @@
FactoryGirl.define do
factory :user do
name { Faker::Name.name }
email { Faker::Internet.email }
password 'secret'
password_confirmation 'secret'
factory :user_with_sequence_number do
sequence(:email){ |n| "email#{n}@example.com" }
sequence(:name){ |n| "username#{n}"}
end
factory :user_name_length_gt_20 do
name '12345678901234567890xxxx'
end
factory :user_email_length_gt_50 do
email '12345678901234567890123456789012345678901234567890@qq.com'
end
factory :user_password_length_lt_6 do
password { Faker::Internet.password( 0, 5 ) }
end
end
end

74
spec/models/user_spec.rb Normal file
View File

@ -0,0 +1,74 @@
require 'rails_helper'
RSpec.describe User, type: :model do
it 'has a valid factory' do
expect(build(:user)).to be_valid
end
describe 'username test' do
it 'does not allow absence of username' do
expect(build(:user, name: nil)).not_to be_valid
end
it 'does not allow duplicate username' do
create(:user_with_sequence_number, name: 'ccx')
user_duplicated_name = build(:user_with_sequence_number, name: 'ccx')
user_duplicated_name.valid?
expect(user_duplicated_name.errors[:name].size).to eq(1)
end
it 'does not allow name length > 20' do
expect(build(:user_name_length_gt_20)).not_to be_valid
end
end
describe 'email test' do
it 'does not allow absence of email' do
expect(build(:user, email: nil)).not_to be_valid
end
it 'does not allow duplicate email' do
create(:user_with_sequence_number, email: '1261138729@qq.com')
user_duplicated_email = build(:user_with_sequence_number, email: '1261138729@qq.com')
user_duplicated_email.valid?
expect(user_duplicated_email.errors[:email].size).to eq(1)
end
it 'does not allow email length > 50' do
expect(build(:user_email_length_gt_50)).not_to be_valid
end
it 'email should match /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i' do
expect(build(:user, email: '123qq.com')).not_to be_valid
expect(build(:user, email: '@qq.com')).not_to be_valid
expect(build(:user, email: '123@qq')).not_to be_valid
expect(build(:user, email: '123@.com')).not_to be_valid
expect(build(:user, email: '123@qq.')).not_to be_valid
end
end
describe 'password test' do
it 'does not allow password length < 6' do
expect(build(:user_password_length_lt_6)).not_to be_valid
end
it 'password_confirmation should match password when password_confirmation not nil' do
expect(build(:user, password: '123456', password_confirmation: '654321')).to_not be_valid
end
it 'do not trigger match when password_confirmation nil' do
expect(build(:user, password: '123456', password_confirmation: nil)).to be_valid
end
it 'does not allow password absence on create' do
expect(build(:user, password: nil, password_confirmation: nil)).to_not be_valid
end
it 'can authenticate' do
user = create(:user, password: '123456', password_confirmation: '123456')
expect(user.authenticate('123456')).to be_instance_of User
expect(user.authenticate('654312')).to be false
end
end
end