Skip to content

Instantly share code, notes, and snippets.

@traviskroberts
Last active August 29, 2015 14:21
Show Gist options
  • Save traviskroberts/a3cb3677eb764b5cd365 to your computer and use it in GitHub Desktop.
Save traviskroberts/a3cb3677eb764b5cd365 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
belongs_to :organization
has_many :projects
validates :name, :presence => true
validates :email, :uniqueness => true
end
require 'spec_helper'
describe User do
context 'associations' do
it { should belong_to(:organization) }
it { should have_many(:projects) }
end
context 'validations' do
it { should validate_presence_of(:name) }
it { should validate_uniqueness_of(:email) }
end
end
class User < ActiveRecord::Base
belongs_to :organization, :class_name => 'UserOrganization'
has_many :contracts
has_many :jobs, :through => :contracts
has_many :projects, :order => 'date DESC', :dependent => :destroy
accepts_nested_attributes_for :projects, :limit => 3
end
require 'spec_helper'
describe User do
context 'associations' do
it { should belong_to(:organization).class_name('UserOrganization') }
it { should have_many(:contracts) }
it { should have_many(:jobs).through(:contracts) }
it { should have_many(:projects).order('date DESC').dependent(:destroy) }
it { should accept_nested_attributes_for(:projects).limit(3) }
end
end
class User < ActiveRecord::Base
validates :name, :length => { :minimum => 10, :maximum => 100 }
validates :email, :format => { :with => /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/ }
validates :status, :inclusion => { :in => %w(active inactive suspended) }
attr_accessible :name, :email
attr_accessible :name, :email, :status, :as => :admin
end
require 'spec_helper'
describe User do
context 'validations' do
it { should ensure_length_of(:name).is_at_least(10).is_at_most(100) }
it { should validate_format_of(:email).with('user@email.com') }
it { should validate_format_of(:email).not_with('user@email') }
it { should ensure_inclusion_of(:status).in_array(['active', 'inactive', 'suspended']) }
end
context 'mass assignment' do
it { should allow_mass_assignment_of(:name) }
it { should allow_mass_assignment_of(:email) }
it { should_not allow_mass_assignment_of(:status) }
it { should allow_mass_assignment_of(:status).as(:admin) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment