From: Ryan Davis Date: 2010-05-27T06:49:09+09:00 Subject: Re: How do I write an RSpec test to unit-test this interesting metaprogramming code? On May 23, 2010, at 07:00 , James Wenton wrote: > So far, so good. I want to write some RSpec tests to verify this code > is actually doing what it's supposed to. But there's a problem! If I > invoke `attr_option :foo` in one of the test methods, that method is > now forever defined in Config. So a subsequent test will fail when it > shouldn't, because `foo` is already defined: > > it "should support a specified option" do > c = Configurator::Config > c.attr_option :foo > # ... > end > > it "should support multiple options" do > c = Configurator::Config > c.attr_option :foo, :bar, :baz # Error! :foo already defined > # by a previous test. > # ... > end Caleb and Robert are nit-picking... 'c' is a perfectly fine name for a variable in a 4 line test/spec. The problem you're having is easily solved by using anonymous subclasses: it "should support a specified option" do c = Class.new(Configurator::Config) c.attr_option :foo # ... end That makes a throwaway class that has all the same features of the superclass without any of the infectious properties of calling your attr methods on the real thing.