[rspec-users] Standardize environment between specs containing class defs

Matt Margolis matt at mattmargolis.net
Thu Sep 20 22:29:57 EDT 2007


I have some specs that involve the use of eval and class definitions to 
test code generation.  I want to always start with a clean slate so none 
of my tests fail or succeed incorrectly due to artifacts left over from 
previous specs. 

Example of my situation
  Spec 1 defines
    class Fish
    class Cod < Fish
  Spec 2 defines
    class Animal
    class Cod < Animal

In this situation the second spec will fail since Cod was previously 
defined to be a subclass of Fish and you can not change the superclass 
of a subclass in ruby without causing an exception.

I want the resetting to be as automatic as possible since calling 
remove_constant on every class my code defines after each spec is a real 
pain since this situation is going to crop up in lots of different specs 
and describe blocks.

Thank you,
Matt Margolis


More information about the rspec-users mailing list