[rspec-users] Removing an AR class definition, for testing plugins

Pat Maddox pergesu at gmail.com
Mon Sep 10 18:12:19 EDT 2007


I'm writing an acts_as_* plugin and am trying to BDD it.  Ideally my
specs would look like:

describe ActsAsCloneable, " basic cloning" do
  load_example_classes
  School.class_eval do
    acts_as_cloneable
  end

  before(:each) do
    @old_school = School.create! :name => "Baylake Pines", :city =>
"Virginia Beach", :guid => "abc123"
    @clone = @old_school.build_clone
  end

  it "should preserve the attributes" do
    @clone.name.should == "Baylake Pines"
    @clone.city.should == "Virginia Beach"
    @clone.guid.should == "abc123"
  end

  it "should not clone the id" do
    @clone.id.should be_blank
  end
end

describe ActsAsCloneable, " while specifying an attribute not to clone" do
  load_example_classes
  School.class_eval do
    acts_as_cloneable :except => :guid
  end

  before(:each) do
    @old_school = School.create! :name => "Baylake Pines", :city =>
"Virginia Beach", :guid => "abc123"
    @clone = @old_school.build_clone
  end

  it "should preserve the regular attributes" do
    @clone.name.should == "Baylake Pines"
    @clone.city.should == "Virginia Beach"
  end

  it "should not clone the specified attribute" do
    @clone.guid.should be_blank
  end

  it "should not clone the uncloned attribute" do
    @clone.guid.should be_blank
  end
end

load_example_classes is a helper method that just defines the AR::base
classes.  The undefs would go there too.

Unfortunately what's happening is that the definition from the second
spec is being used for the first spec...so when running both specs,
the first fails.  However if I comment out the second spec, the first
one passes.

I've tried using remove_const and Object.remove_class.  Neither seems
to work.  I need to start with a fresh class definition before each
description is run...how can I do that?

Pat


More information about the rspec-users mailing list