require 'spec_helper'

describe User do

  let(:user) { FactoryGirl.create(:user) }

  it 'has a valid factory' do
    user.should be_valid
  end

  it 'is invalid without email, or invalid email' do
    FactoryGirl.build(:user, email: nil).should_not be_valid

    FactoryGirl.build(:user, email: 'nil@').should_not be_valid
  end

  it 'is invalid without password' do
    FactoryGirl.build(:user, password: nil).should_not be_valid
  end

  it 'accept a blank password if the user exists' do
    user.password = ''
    user.should be_valid
    user.save.should be_true
  end

  it 'is invalid if the email exists' do
    new_user = FactoryGirl.build(:user, email: user.email)
    new_user.should_not be_valid
    new_user.should have(1).error_on(:email)
  end

  it "can't delete the last admin" do
    expect { user.destroy }.to raise_error 'Must be at least one admin'
  end
end

require 'spec_helper'

describe User do

  describe '#valid?' do
    subject { FactoryGirl.build(:user, params) }

    context 'default factory' do
      let(:params) { nil }

      it { should be_valid }
    end

    context 'without email' do
      let(:params) { {email: nil} }

      it { should_not be_valid }
    end

    context 'with invalid email' do
      let(:params) { {email: 'nil@'} }

      it { should_not be_valid }
    end

    context 'without password' do
      let(:params) { {password: nil} }

      it { should_not be_valid }
    end

    context 'is invalid if the email exists' do
      let(:params) { {password: nil} }
      let!(:existing_user) { FactoryGirl.create(:user, email: subject.email) }

      it { should_not be_valid }
      it { should have(1).error_on(:email) }
    end
  end

  describe '#save' do
    subject { FactoryGirl.create(:user) }

    context 'accept a blank password if the user exists' do
      before { subject.password = '' }

      it { should be_valid }
      it { expect(subject.save).to be_true }
    end
  end

  describe '#destroy' do
    subject { FactoryGirl.create(:user) }

    context "can't delete the last admin" do
      it { expect { subject.destroy }.to raise_error 'Must be at least one admin' }
    end
  end
end

Refactor User specs for readability
Refactor User specs for readability
