Skip to content

Instantly share code, notes, and snippets.

@gkop
Created March 17, 2015 17:23
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 gkop/18d8c6497d84948c2099 to your computer and use it in GitHub Desktop.
Save gkop/18d8c6497d84948c2099 to your computer and use it in GitHub Desktop.
Spec for Population mixin that counts instances of class and its subclasses
class Animal
include Population
end
class Kangaroo < Animal
end
class CaptainKangaroo < Kangaroo
end
RSpec.describe "Counting populations of classes" do
it "Counts the population of the top-most class" do
expect(Animal.population).to eq(0)
Animal.new
expect(Animal.population).to eq(1)
expect(Animal.population).to eq(1)
Animal.new
expect(Animal.population).to eq(2)
end
it "Counts the population of subclasses, also" do
expect(Animal.population).to eq(0)
expect(Kangaroo.population).to eq(0)
expect(CaptainKangaroo.population).to eq(0)
Kangaroo.new
expect(Kangaroo.population).to eq(1)
expect(Animal.population).to eq(1)
expect(CaptainKangaroo.population).to eq(0)
Animal.new
expect(Animal.population).to eq(2)
expect(Kangaroo.population).to eq(1)
expect(CaptainKangaroo.population).to eq(0)
CaptainKangaroo.new
expect(Animal.population).to eq(3)
expect(Kangaroo.population).to eq(2)
expect(CaptainKangaroo.population).to eq(1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment