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

David Chelimsky dchelimsky at gmail.com
Mon Aug 6 20:47:46 EDT 2007


On 8/6/07, ben at benburkert.com <ben at benburkert.com> wrote:
> 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
>

The way you're approaching it means that if something changes about
the initializer for the SiberianTiger that doesn't change for the
initializer of the SiberianCat, you're kinda screwed. I'd do it like
this: http://pastie.caboo.se/85444

This keeps the shared behaviour very simple (all it needs is an
instance variable) and puts control of the object you're dealing with
in the spec.

WDYT?


>
>
>
> Then again, i have a gut feeling that if i took better advantage of
> modules it wouldn't be an issue...
>
> -Ben
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list