Skip to content

Instantly share code, notes, and snippets.

@samflores
Created March 19, 2009 13:30
Show Gist options
  • Save samflores/81816 to your computer and use it in GitHub Desktop.
Save samflores/81816 to your computer and use it in GitHub Desktop.
## task_spec.rb
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Task do
include ValidTaskAttributes
# o módulo ValidTaskAttributes define o método valid_task que retorna
#{
# :name => "Task name",
# :description => "Task description",
# :alias => "task_alias",
# :due_date => Date.today,
# :priority => 1,
# :percentage => 9.99,
#}
describe 'validations' do
subject { Task.create(valid_task) }
#aqui eu crio um objeto do tipo Task no banco. ele está sendo salvo com sucesso
it { should validate_presence_of(:name) }
it { should validate_presence_of(:alias) }
it { should validate_uniqueness_of(:name) }
it { should validate_uniqueness_of(:alias) }
it { should validate_numericality_of(:priority, :only_integer => true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 10) }
it { should validate_numericality_of(:percentage, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100, :allow_nil => true) }
end
describe 'associations' do
subject { Task.create(valid_task) }
it { should belong_to(:parent, :class_name => 'Task') }
it { should have_many(:children, :class_name => 'Task', :foreign_key => 'parent_id') }
end
end
## task.rb
class Task < ActiveRecord::Base
belongs_to :parent, :class_name => 'Task'
has_many :children, :class_name => 'Task', :foreign_key => 'parent_id'
validates_presence_of :name
validates_presence_of :alias
validates_uniqueness_of :name
validates_uniqueness_of :alias
validates_numericality_of :priority, :only_integer => true, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 10
validates_numericality_of :percentage, :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100, :allow_nil => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment