[rspec-users] used the described Class in a shared behavior

ben at benburkert.com ben at benburkert.com
Mon Aug 6 17:25:55 EDT 2007


Is it possible to access the described class in a shared behavior? I'm
trying to do something like this:

describe "Siberian feline", :shared => true do
  described_class_instance_as :feline, :name => "fluffy", :breed =>
"Siberian"

  # or maybe

  before(:all) do
    @feline = described_class.new(:name => "fluffy", :breed => "Siberian")
  end

  it "should have long hair" do
    @feline.should be_long_haired
  end
end

describe SiberianCat, "(a subclass of, say, Cat)" do
  it_should_behave_like "Siberian feline"

  it "should purr" do
    @feline.sound.should == "purr"
  end
end

describe SiberianTiger, "(a subclass of BigCat)" do
  it_should_behave_like "Siberian feline"

  it "should roar" do
    @feline.sound.should == "roar"
  end
end



I'm looking for something more elegant than my current way, which is like so:

describe "Siberian felines", :shared => true do
  before(:all) do
    @feline = @klass.new(:name => "fluffy", :breed => "Siberian")
  end

  it "should have long hair" do
    @feline.should be_long_haired
  end
end

describe SiberianCat, "(a subclass of, say, Cat)" do
  it_should_behave_like "Siberian felines"

  before(:all) do
    @klass = SiberianCat
  end

  it "should purr" do
    @feline.sound.should == "purr"
  end
end

describe SiberianTiger, "(a subclass of BigCat)" do
  it_should_behave_like "Siberian felines"

  before(:all) do
    @klass = SiberanTiger
  end

  it "should roar" do
    @feline.sound.should == "roar"
  end
end




Then again, i have a gut feeling that if i took better advantage of
modules it wouldn't be an issue...

-Ben



More information about the rspec-users mailing list