Skip to content

Instantly share code, notes, and snippets.

@rodrigoulisses
Created February 15, 2013 18:04
Show Gist options
  • Save rodrigoulisses/4962169 to your computer and use it in GitHub Desktop.
Save rodrigoulisses/4962169 to your computer and use it in GitHub Desktop.
class PrecatoryParcel < Compras::Model
attr_accessible :expiration_date, :value, :situation, :payment_date
attr_accessible :amount_paid, :observation
has_enumeration_for :situation, :with => PrecatoryParcelSituation
belongs_to :precatory
delegate :pledges, :to => :precatory, :prefix => true, :allow_nil => true
validates :expiration_date, :value, :situation, :presence => true
before_destroy :check_destroy
def check_destroy
if precatory_pledges.any?
errors.add(:base, :cannot_be_changed_if_precatory_have_pledge)
end
end
end
# encoding: utf-8
require 'model_helper'
require 'app/models/precatory_parcel'
require 'app/enumerations/precatory_parcel_situation'
describe PrecatoryParcel do
it { should validate_presence_of :expiration_date }
it { should validate_presence_of :value }
it { should validate_presence_of :situation }
it { should belong_to :precatory }
context "destroy precatory_parcel" do
before do
subject.expiration_date = Date.new(2013, 01, 31)
subject.value = 100
subject.situation = PrecatoryParcelSituation::PAID
subject.stub(:precatory).and_return(double("Precatorio").stub(:pledges).and_return(double("Emprenho"), :any? => true))
end
it "when precatory has pledge" do
subject.destroy
expect(subject.errors[:base]).to include 'não pode ser alterada. Este Precatório já possui Empenho'
end
end
end
@dreoliv
Copy link

dreoliv commented Feb 15, 2013

faz assim:

Caso inválido:

subject.stub(:precatory_pledges => [])

Caso válido:

subject.stub(:precatory_pledges => [double])

Não esquece de retornar false depois de incluir o erro, senão ele vai excluir o registro do mesmo jeito.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment