Pat Maddox
2007-Sep-10 22:12 UTC
[rspec-users] Removing an AR class definition, for testing plugins
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
Carl Porth
2007-Sep-10 23:40 UTC
[rspec-users] Removing an AR class definition, for testing plugins
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
Pablo Castellazzi
2007-Sep-19 07:23 UTC
Re: Removing an AR class definition, for testing plugins
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) do2) 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 [] On Sep 10, 5:12 pm, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---