Created
April 15, 2009 16:09
-
-
Save neves/95875 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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 |
This file contains hidden or 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
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