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

2007-08-06 Thread ben
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

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


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

2007-08-06 Thread David Chelimsky
On 8/6/07, David Chelimsky [EMAIL PROTECTED] wrote:
 On 8/6/07, [EMAIL PROTECTED] [EMAIL PROTECTED] 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?

Also - side note - before(:each) is preferred over before(:all)
because it's better to be sure that each example starts with a clean
slate. If one should change the state of the object you're describing,
you could end up with unexpected results and hair-pulling debugging
sessions.



 
 
 
  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@rubyforge.org
  http://rubyforge.org/mailman/listinfo/rspec-users
 

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users