Skip to content

Instantly share code, notes, and snippets.

@miguelperez
Created June 5, 2012 18:39
Show Gist options
  • Save miguelperez/2876807 to your computer and use it in GitHub Desktop.
Save miguelperez/2876807 to your computer and use it in GitHub Desktop.
Model Conventions
This conventions are the ones I use when developing models.
Basically all the rspec generated file is separated in sections depending on the things the model does, validates and sets.
Validations
Relations
instance methods
scopes
All of those tested separately.
class Person < ActiveRecord::Base
ALLOWED_ROLES = %w(
role_1
role_2
)
belongs_to :project
validates :role, presence: true, inclusion: {in: ALLOWED_ROLES}
validates :name, presence: true
def human_role
self.role.humanize
end
end
require 'spec_helper'
describe Person, "Relations" do
it { should belong_to(:project) }
end
describe Person, "Validations" do
subject { FactoryGirl.create(:checkin) }
it { should validate_presence_of(:role) }
it { should validate_presence_of(:name) }
it "should allow valid values for ##role##" do
Checkin::ALLOWED_ROLES.each do |role|
should allow_value(role).for(:role)
end
end
it { should_not allow_value("other").for(:role) }
end
describe Person, "instance methods" do
describe "human role" do
it "should return the humanized string..." do
...
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment