# Removing an AR class definition, for testing plugins

**URL:** <https://www.ruby-forum.com/t/removing-an-ar-class-definition-for-testing-plugins/114295>\
**Category:** Rails\
**Created:** [September 11, 2007, 12:13am UTC](https://www.ruby-forum.com/t/removing-an-ar-class-definition-for-testing-plugins/114295 "2007-09-11T00:13:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 11, 2007, 12:13am UTC](https://www.ruby-forum.com/t/removing-an-ar-class-definition-for-testing-plugins/114295/1 "2007-09-11T00:13:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [September 19, 2007, 9:23am UTC](https://www.ruby-forum.com/t/removing-an-ar-class-definition-for-testing-plugins/114295/2 "2007-09-19T09:23:52Z")

</div>

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 []
