Matt Margolis
2007-Sep-21 02:29 UTC
[rspec-users] Standardize environment between specs containing class defs
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
aslak hellesoy
2007-Sep-21 06:53 UTC
[rspec-users] Standardize environment between specs containing class defs
You can have a global after block in your spec_helper.rb that undefines all such constants. Then you just have to remember to add these constants to some global array whenever you define them. a On 9/21/07, Matt Margolis <matt at mattmargolis.net> wrote:> 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Dan North
2007-Sep-21 09:42 UTC
[rspec-users] Standardize environment between specs containing class defs
Although it would be nice (and not too difficult) to have the example runner intercept any class creation (and perhaps other global/constant definitions) and undef them after each example. It certainly seems reasonable to have any defined classes go away after an example. aslak hellesoy wrote:> You can have a global after block in your spec_helper.rb that > undefines all such constants. Then you just have to remember to add > these constants to some global array whenever you define them. > > a > > On 9/21/07, Matt Margolis <matt at mattmargolis.net> wrote: > >> 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 >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users >> >> > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070921/7f9674b4/attachment.html
Mark Van De Vyver
2007-Oct-04 08:52 UTC
[rspec-users] Standardize environment between specs containing class defs
On 9/21/07, Dan North <dan at tastapod.com> wrote:> > Although it would be nice (and not too difficult) to have the example > runner intercept any class creation (and perhaps other global/constant > definitions) and undef them after each example. > > It certainly seems reasonable to have any defined classes go away after an > example.Hmm, this is also causing me some trouble - do doubt a sign of badly designed specs. Anyway, I have a class defined in before, and a variation of it define in the following example, so I see this ''contamination'' between examples....> > aslak hellesoy wrote: > You can have a global after block in your spec_helper.rb that > undefines all such constants. Then you just have to remember to add > these constants to some global array whenever you define them.OK, I''ve no idea what that means in practice - can anyone shed some light? Ideally I think that I''d like to remove the class definition ''after (:all)'' in each example. Can any one offer a recommended (simple?) solution? Thanks Mark> > a > > On 9/21/07, Matt Margolis <matt at mattmargolis.net> wrote: > > > 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 > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >