# Inheritance Question

**URL:** <https://www.ruby-forum.com/t/inheritance-question/86304>\
**Category:** Ruby\
**Created:** [March 23, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304 "2006-03-23T21:37:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/1 "2006-03-23T21:37:20Z")

</div>

Hello,

I’m having difficulty understanding inheritance in this case. In the  
program below, I’m inheriting from \<Test::Unit::TestCase. I also want my  
other class in this program to inherit from \<Test::Unit::TestCase. My  
goal is to be able to make assertions in both classes.

If a run the program as written below, it works.

require ‘test/unit’  
require ‘test/unit/ui/console/testrunner’

class TC\_MyTest \< Test::Unit::TestCase  
def test\_myMethod  
puts ‘test 1’  
assert(true, ‘test 1’)  
@test = TC\_MyClass.new()  
@test\_new = @test.test\_myTestCase  
end  
End

class TC\_MyClass  
def initialize  
end  
def test\_myTestCase  
puts ‘test 2’  
#assert(true, ‘test 2’)  
puts ‘test 3’  
puts ‘test 4’  
end  
end

But if try to do this, I get an error:

class TC\_MyTest \< Test::Unit::TestCase  
def test\_myMethod  
puts ‘test 1’  
assert(true, ‘test 1’)  
@test = TC\_MyClass.new()  
@test\_new = @test.test\_myTestCase  
end  
End

## class TC\_MyClass \< Test::Unit::TestCase def initialize end def test\_myTestCase puts ‘test 2’ assert(true, ‘test 2’) puts ‘test 3’ puts ‘test 4’ end end

c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:51:in `initialize’: wrong  
number of arguments (1 for 0) (ArgumentError)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/2 "2006-03-23T21:46:43Z")

</div>

On 3/23/06, David S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> I’m having difficulty understanding inheritance in this case. In the  
> program below, I’m inheriting from \<Test::Unit::TestCase. I also want my  
> other class in this program to inherit from \<Test::Unit::TestCase.

There’s no problem with having multiple classes in the same source  
file inherit from some other class.

> ```
> puts 'test 1'
> puts 'test 2'
> puts 'test 1'
> puts 'test 2'
> assert(true, 'test 2')
> puts 'test 3'
> puts 'test 4'
> 
> ```
> 
> ## end end
> 
> c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:51:in `initialize’: wrong  
> number of arguments (1 for 0) (ArgumentError)

You don’t need the initialize method in TC\_MyClass. Delete that and  
see if it works.

There’s still something strange going on here though. Why do you want  
to put asserts in the class being tested? Normally all the asserts go  
in the class doing the testing. Also, normally the classes being  
tested and the test classes are in separate source files.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/3 "2006-03-23T21:49:25Z")

</div>

This error is related to an earlier post.  
Test::Unit::TestCase#initialize takes one argument.

require ‘test/unit’

class FooTester \< Test::Unit::TestCase  
def initialize(foo)  
super(foo)  
end  
def test\_one  
assert(true)  
end  
end

see the earlier post for a much better explination by Ross. 🙂

- Geoff

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/4 "2006-03-23T22:30:35Z")

</div>

David S. wrote:

> require ‘test/unit/ui/console/testrunner’  
> class TC\_MyClass  
> But if try to do this, I get an error:  
> class TC\_MyClass \< Test::Unit::TestCase  
> c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:51:in `initialize’: wrong  
> number of arguments (1 for 0) (ArgumentError)

i know people already said this, but I’m saying it again cause it might  
get lost in the noise.

remove the initialize method

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/5 "2006-03-23T22:09:41Z")

</div>

On Fri, 2006-03-24 at 05:37 +0900, David S. wrote:

> Hello,
> 
> I’m having difficulty understanding inheritance in this case. In the  
> program below, I’m inheriting from \<Test::Unit::TestCase. I also want my  
> other class in this program to inherit from \<Test::Unit::TestCase. My  
> goal is to be able to make assertions in both classes.

If all you want to do is make assertions, you could try something like:

require ‘test/unit/assertions’

class MyClass  
include Test::Unit::Assertions

def something(something\_else)  
assert\_equal 2, something\_else.length  
end  
end

Subclassing Test::Unit::TestCase will rarely work out well since I  
believe Test::Unit automatically collects testcases and runs them before  
ruby quits.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 23, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/inheritance-question/86304/6 "2006-03-23T23:54:29Z")

</div>

I noticed that removing the initialize method allows the tests to run.  
Is there something in test /unit that requires an initialize  
definition to have a certain interface?

-Justin

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [March 24, 2006, 2:37am UTC](https://www.ruby-forum.com/t/inheritance-question/86304/7 "2006-03-24T02:37:56Z")

</div>

Thanks Everybody for your help. So I did get it to work by removing the  
initialize method with the classes in one source file. When I seperated  
them into their own files, I got this message:

C:/automation/src/thdf/projects/core/testharness/fixtures/admin\_administration/test.rb:11:in  
`test\_myMethod’

1 tests, 1 assertions, 0 failures, 1 errors

I know some of you are wondering why I want to do this?

I’m writing automated scripts using Watir to drive a web app. The app  
I’m testing has many repeatable steps. My goal is to break out the  
repeatable patterns into it’s own classes and call them as needed. I  
need to be able to make assertions in the those classes. So when I  
started to think about how I can do this, I came up with the program  
above to see if this was possible. So far it’s been touch and go.
