Skip to content

Instantly share code, notes, and snippets.

@satococoa
Last active December 18, 2015 14:19
Show Gist options
  • Save satococoa/5796189 to your computer and use it in GitHub Desktop.
Save satococoa/5796189 to your computer and use it in GitHub Desktop.
User モデル
class User < ActiveRecord::Base
has_secure_password
validates :email, presence: true, uniqueness: true
validates :password, length: { minimum: 6, if: ->(u) { u.password.present? } }
end
require 'spec_helper'
describe User do
describe '#valid?' do
context 'email, password, password_confirmation が与えられたとき' do
subject(:user) { FactoryGirl.build(:user) }
specify { expect(subject).to be_valid }
end
context 'password, password_confirmation が違うとき' do
subject(:user) { FactoryGirl.build(:user, password_confirmation: 'wrongpassword') }
specify { expect(subject).not_to be_valid }
end
context 'password_confirmation が空のとき' do
subject(:user) { FactoryGirl.build(:user, password_confirmation: nil) }
specify { expect(subject).not_to be_valid }
end
context 'email が空のとき' do
subject(:user) { FactoryGirl.build(:user, email: nil) }
specify { expect(subject).not_to be_valid }
end
context 'email が既に使用されているものであるとき' do
subject(:user) { FactoryGirl.build(:user) }
before do
FactoryGirl.create(:user, email: user.email)
end
specify { expect(subject).not_to be_valid }
end
context 'passwordが6文字未満のとき' do
subject(:user) { FactoryGirl.build(:user, password: 'foo') }
specify { expect(subject).not_to be_valid }
end
end
describe '#update' do
subject(:user) { FactoryGirl.create(:user) }
context 'emailを更新するとき' do
let(:attrs) { {email: '[email protected]'} }
it 'updateは成功する' do
expect(user.update(attrs)).to be_true
end
it 'emailが更新できる' do
expect { user.update(attrs) }.to change { user.email }
end
end
context 'password_confirmationが与えられないとき' do
let(:attrs) { {password: 'newpassword'} }
it 'updateは失敗する' do
expect(user.update(attrs)).not_to be_true
end
end
context 'password, password_confirmationに同じ値が与えられたとき' do
let(:attrs) { {password: 'newpassword', password_confirmation: 'newpassword'} }
it 'updateは成功する' do
expect(user.update(attrs)).to be_true
end
it 'passwordが更新できる' do
expect { user.update(attrs) }.to change { user.password_digest }
end
end
context 'email, passwordを一気に更新' do
let(:attrs) {
{email: '[email protected]', password: 'newpassword', password_confirmation: 'newpassword'}
}
it 'updateは成功する' do
expect(user.update(attrs)).to be_true
end
it 'emailが更新できる' do
expect { user.update(attrs) }.to change { user.email }
end
it 'passwordが更新できる' do
expect { user.update(attrs) }.to change { user.password_digest }
end
end
end
describe '#authenticate' do
subject(:user) { FactoryGirl.create(:user) }
context 'password が正しいとき' do
specify { expect(user.authenticate(user.password)).to be_true }
end
context 'password が間違えているとき' do
specify { expect(user.authenticate(user.password + 'wrong')).not_to be_true }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment