I''m trying to add some functionality to the setup method of all my tests. I added a method called setup_with_ts in the test_helper and chained it using alias_method_chain :setup, :ts and that works great for every test that does not define setup in the test. So for my unit tests i tried also defining the same method setup_with_ts in a class ActiveSupport::TestCase def setup_with_ts puts "setup with ts" setup_without_ts end alias_method_chain :setup, :ts end it was just a test to see what would happen. if i called this method in the setup of a test like : def setup setup_with_ts . . . end that will call it just fine so the method exists. but if i remove the setup_with_ts line and run a test the setup method in the test runs, but it never runs my aliased chained method. i''m not sure why the alias_method_chain seems to work for test with no setup method and not for one that has a setup method defined. Any help would be great. Thanks. Erik
Just an update: It is some kind of order of file loading or something. If i put def setup_with_ts puts "setup with ts" setup_without_ts end alias_method_chain :setup, :ts that directly into a test with a setup method it works fine, but not if i define it in the test_helper.rb Erik On Oct 21, 11:38 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> I''m trying to add some functionality to the setup method of all my > tests. > > I added a method called setup_with_ts in the test_helper and chained > it using alias_method_chain :setup, :ts and that works great for every > test that does not define setup in the test. > > So for my unit tests i tried also defining the same method > setup_with_ts in a > class ActiveSupport::TestCase > def setup_with_ts > puts "setup with ts" > setup_without_ts > end > alias_method_chain :setup, :ts > end > > it was just a test to see what would happen. > > if i called this method in the setup of a test like : > def setup > setup_with_ts > . > . > . > end > > that will call it just fine so the method exists. but if i remove the > setup_with_ts line and run a test the setup method in the test runs, > but it never runs my aliased chained method. > > i''m not sure why the alias_method_chain seems to work for test with no > setup method and not for one that has a setup method defined. Any > help would be great. > > Thanks. > Erik
Another update: if i do: class ActiveSupport::TestCase def setup_with_ts puts "setup with ts" setup_without_ts end alias_method_chain :setup, :ts end and then do puts self.methods in one of my tests it shows those methods exist. but they do not get called unless i define setup_with_ts and alias it in each test... On Oct 21, 11:50 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> Just an update: > > It is some kind of order of file loading or something. > > If i put > def setup_with_ts > puts "setup with ts" > setup_without_ts > end > alias_method_chain :setup, :ts > > that directly into a test with a setup method it works fine, but not > if i define it in the test_helper.rb > > Erik > > On Oct 21, 11:38 am, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote: > > > I''m trying to add some functionality to the setup method of all my > > tests. > > > I added a method called setup_with_ts in the test_helper and chained > > it using alias_method_chain :setup, :ts and that works great for every > > test that does not define setup in the test. > > > So for my unit tests i tried also defining the same method > > setup_with_ts in a > > class ActiveSupport::TestCase > > def setup_with_ts > > puts "setup with ts" > > setup_without_ts > > end > > alias_method_chain :setup, :ts > > end > > > it was just a test to see what would happen. > > > if i called this method in the setup of a test like : > > def setup > > setup_with_ts > > . > > . > > . > > end > > > that will call it just fine so the method exists. but if i remove the > > setup_with_ts line and run a test the setup method in the test runs, > > but it never runs my aliased chained method. > > > i''m not sure why the alias_method_chain seems to work for test with no > > setup method and not for one that has a setup method defined. Any > > help would be great. > > > Thanks. > > Erik
On Oct 21, 7:38 pm, erik <e...-9T/GIyPsA//QT0dZR+AlfA@public.gmane.org> wrote:> > i''m not sure why the alias_method_chain seems to work for test with no > setup method and not for one that has a setup method defined. Any > help would be great.In a nutshell, because there is no reason it should. If a class overrides the superclass implementation of a method then that superclass implementation is not called unless the new implementation explicitly does it. You should be able to do def setup super ... end However you don''t need to do all this, because ActiveSupport::TestCase already handles this sort of stuff. If you do class ActiveSupport::TestCase def setup_with_ts end setup :setup_with_ts end then your method will get called at the appropriate time (ActiveSupport sets up a callback chain that is run just before setup is called) Fred
Reasonably Related Threads
- undefined method `alias_method_chain' for I18n::Backend::Simple:Class
- alias_method_chain and Class methods (a Ruby question)
- undefined method ''alias_method_chain''
- alias_method_chain in Facebooker Causing Stack Level Too Deep
- alias_method_chain stack level too deep in Rake test only