Removing an AR class definition, for testing plugins

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

The main idea behind the solution is classes are constants, knowing
that and
also that every class inherit from Module, as you guessed remove_const
method
is the key to do it.

  1. If the class you want to remove belongs to main you should do:
    Object.send :remove_const, ‘ClassToRemove’
acts_as_cloneable

end

before(:each) do

  1. If the class you want to remove is inside a class or module you
    should do:
    MyModule.remove_const, ‘ClassToRemove’ => outside the module/class
    self.class.send :remove_const, ‘ClassToRemove’ => inside the module/
    class

Assuming you create your test class with an eval inside a test case
something like this should work:

class MyTestCase
def setup
eval ‘class B; end’
end

def teardown
self.class.send :remove_const, ‘B’
end
end

a = MyTestCase.new
a.setup ; puts “after setup #{MyTestCase.constants.inspect}”

output [“B”]

a.teardown ; puts “after teardown #{MyTestCase.constants.inspect}” #
output []