Created
June 5, 2012 18:39
-
-
Save miguelperez/2876807 to your computer and use it in GitHub Desktop.
Model Conventions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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