# Other way to send message to parent class in unit testing

**URL:** <https://www.ruby-forum.com/t/other-way-to-send-message-to-parent-class-in-unit-testing/78218>\
**Category:** Ruby\
**Created:** [December 4, 2006, 5:22am UTC](https://www.ruby-forum.com/t/other-way-to-send-message-to-parent-class-in-unit-testing/78218 "2006-12-04T05:22:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rinda](https://www.ruby-forum.com/u/rinda)\
**Post date:** [December 4, 2006, 5:22am UTC](https://www.ruby-forum.com/t/other-way-to-send-message-to-parent-class-in-unit-testing/78218/1 "2006-12-04T05:22:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce73a5/32.png) [@rinda](https://www.ruby-forum.com/u/rinda)\
**Post date:** [December 4, 2006, 5:26am UTC](https://www.ruby-forum.com/t/other-way-to-send-message-to-parent-class-in-unit-testing/78218/2 "2006-12-04T05:26:23Z")

</div>

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  
> ……
