Other way to send message to parent class in unit testing

Hi, I have 8 class that need to be tested. They all have some same
method to be tested. So I make parent class to do the duplicate work.

This is the parent class:

class BaseElementControllerTest < Test::Unit::TestCase

def setup
# notice this line
@controller = @con_name.new
# end of notice this line

@request    = ActionController::TestRequest.new
@response   = ActionController::TestResponse.new

end

def test_new
get :new
assert_response :success
end

end

The 8 class have same method that need to be tested, that is test_new.
The child class is like this:

require File.dirname(FILE) + ‘/…/test_helper’
require ‘thermo_detector_controller’

Re-raise errors caught by the controller.

class ThermoDetectorController; def rescue_action(e) raise e end; end

notice this line

@con_name = ThermoDetectorController

end of notice this line

class ThermoDetectorControllerTest < BaseElementControllerTest
fixtures :thermo_detector_order_letters, :workers

specific method to be tested


end

Another child class:

require File.dirname(FILE) + ‘/…/test_helper’
require ‘quartz_heater_controller’

Re-raise errors caught by the controller.

class QuartzHeaterController; def rescue_action(e) raise e end; end

notice this line

@con_name = QuartzHeaterController

end of notice this line

class QuartzHeaterControllerTest < BaseElementControllerTest
fixtures :quartz_heater_order_letters, :workers

specific method to be tested


end

In this case, I send message to parent class using @con_name variable,
instantiated before child class definition. I have to send message
because in parent class, @controller variable must be instantiated
according to child class. So I wonder if there is another way to send
message to parent in unit testing? I try to use initialize method, but
unit testing “forbid” it. Or this is the only way to send message to
parent class in unit testing?

Thank you.

Edit message:
My mistake. @con_name should be $con_name and you can put $con_name in
child class definition

akbarhome wrote:

Hi, I have 8 class that need to be tested. They all have some same
method to be tested. So I make parent class to do the duplicate work.

This is the parent class:

class BaseElementControllerTest < Test::Unit::TestCase

def setup
……