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

Carl Porth badcarl at gmail.com
Mon Sep 10 19:40:05 EDT 2007


You can do something like in rspec/spec/spec/mocks/stub_spec.rb where  
the class is defined in before(:each).  This way your specs aren't  
dependent on an external class definition.

Carl

On Sep 10, 2007, at 3:12 PM, Pat Maddox wrote:

> 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
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list