Why startup and shutdown is not being called?

startup and shudown method is being called if it is inside the one class
only. But if i put that into module and include this module in another
class as shown below startup and shutdown method is not being called.
module BaseClass
class << self
def startup
p “startup”
end

def shutdown
  p "shutdown"
end

end

def setup
puts “setup called”
end

def teardown
puts “teardown called”
end

def cleanup
p “cleanup”
end
end

DemoTest1.rb

require ‘rubygems’
gem ‘test-unit’
require ‘test/unit’
require “BaseClass”

class DemoTest1 < Test::Unit::TestCase
include BaseClass

def test_third()
puts “third”
end
end

now if i execute the DemoTest1.rb I am getting below output
Started
setup called
third
“cleanup”
teardown called

startup and shutdown method is not being called. And i don’t know why??
any idea guys ???

On Fri, Jul 29, 2011 at 9:12 AM, Gaurang S. [email protected]
wrote:

startup and shutdown method is not being called. And i don’t know why??
any idea guys ???

Modules don’t work that way. You’re defining startup and shutdown on
BaseClass’ singleton class, which is not pointed to by DemoTest1’s
singleton
class. You should ether define those methods in another module and then
extend DemoTest1, like this:

module BaseClassClassMethods
def startup
p “startup”
end

def shutdown
p “shutdown”
end
end

module BaseClassInstanceMethods
def setup
p “setup”
end

def teardown
p “teardown”
end

def cleanup
p “cleanup”
end
end

require ‘rubygems’
gem ‘test-unit’
require ‘test/unit’

class DemoTest1 < Test::Unit::TestCase
include BaseClassInstanceMethods
extend BaseClassClassMethods

def test_third()
puts “third”
end
end

Or you could hook into the inclusion like this:

module BaseClass
def self.included(klass)
klass.instance_eval do
def startup
p “startup”
end

  def shutdown
    p "shutdown"
  end
end

end

def setup
p “setup”
end

def teardown
p “teardown”
end

def cleanup
p “cleanup”
end
end

require ‘rubygems’
gem ‘test-unit’
require ‘test/unit’

class DemoTest1 < Test::Unit::TestCase
include BaseClass

def test_third()
puts “third”
end
end