Skip to content

Instantly share code, notes, and snippets.

@neves
Created April 15, 2009 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neves/95875 to your computer and use it in GitHub Desktop.
Save neves/95875 to your computer and use it in GitHub Desktop.
class Animal
@@total = 0
def initialize
increment_total
end
def self.total
@@total
end
def increment_total
@@total += 1
end
def self.inherited(klass)
klass.class_eval <<-CODE
@@#{klass}_total = 0
def increment_total
super()
@@#{klass}_total += 1
end
def #{klass}.total; @@#{klass}_total; end
CODE
end
end
class Cachorro < Animal; end
class Gato < Animal; end
class Siames < Gato; end
describe Animal do
before(:each) do
Animal.new
end
it "ao criar um Animal, o contador dos filhoes deve permanecer 0" do
lambda { Animal.new }.should_not change(Gato, :total)
end
it "ao criar um Animal, o contador dele deve ser 1" do
lambda { Animal.new }.should change(Animal, :total).by(1)
end
it "ao criar um filho, o contador do animal deve ser incrementado" do
lambda { Gato.new }.should change(Animal, :total).by(1)
end
it "ao criar uma instância de uma classe filha, o contador das demais classes filhas nao deve ser alterado" do
lambda { Cachorro.new }.should_not change(Gato, :total)
end
it "ao criar um siames, o contador do Gato deve ser incrementado" do
lambda { Siames.new }.should change(Gato, :total).by(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment