# STI and unit testing

**URL:** <https://www.ruby-forum.com/t/sti-and-unit-testing/77966>\
**Category:** Rails\
**Created:** [March 7, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/sti-and-unit-testing/77966 "2006-03-07T19:36:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [March 7, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/sti-and-unit-testing/77966/1 "2006-03-07T19:36:20Z")

</div>

Hi all, I’m having a bit of a problem with unit testing. I’ve simplified  
my  
models to try and get my head round it but I’m still struggling. I’m  
using  
STI and trying to write tests for the extended models (code below). I’ve  
written test cases for the extended models which themselves extend the  
test  
case for the parent model, so BarTest extends FooTest which extends  
TestCase

when I run ‘bar\_test.rb’ it seems to run the test case twice, once for  
FooTest and once for BarTest, even though the only file I’m running is  
bar\_test.rb - here’s the output:

C:\WINDOWS\system32\cmd.exe /c ruby bar\_test.rb  
Loaded suite bar\_test  
Started  
.Bar  
.Foo  
.  
Finished in 0.109 seconds.

3 tests, 3 assertions, 0 failures, 0 errors

shouldn’t BarTest only have two tests? test\_say\_hi() which is inherited  
from  
FooTest and test\_say\_hello(), I don’t understand why the parent test  
case is  
also being run. Can anyone shed some light on this please?

thanks in advance  
alan

code (also pasted to [Best Low-Code Platforms To Use in 2023 | AppMaster](http://www.pasteserver.net/paste/show/152)):  
1 # model Foo, created using generator  
2  
3 class Foo \< ActiveRecord::Base  
4  
5 def say\_hi  
6 “hi”  
7 end  
8  
9 end  
10  
11 # model Bar, created manually and saved in bar.rb  
12 class Bar \< Foo  
13  
14 def say\_hello  
15 “hello”  
16 end  
17  
18 end  
19  
20 # Test case for model Foo  
21 require File.dirname( **FILE** ) + ‘/…/test\_helper’  
22  
23 class FooTest \< Test::Unit::TestCase  
24 fixtures :foos  
25  
26 def class\_to\_test  
27 Foo  
28 end  
29  
30 def test\_say\_hi  
31 assert\_equal “hi”, class\_to\_test.new.say\_hi  
32  
33 # debug  
34 puts class\_to\_test  
35 # end  
36 end  
37  
38 end  
39  
40 # test case for model Bar, saved in bar\_test.rb  
41 #require File.dirname( **FILE** ) + ‘/…/test\_helper’  
42  
43 require File.dirname( **FILE** ) + ‘/foo\_test’  
44  
45 class BarTest \< FooTest  
46  
47 def class\_to\_test  
48 Bar  
49 end  
50  
51 def test\_say\_hello  
52 assert\_equal “hello”, class\_to\_test.new.say\_hello  
53 end  
54  
55 endsql:CREATE TABLE `foos` ( `id` mediumint(9) NOT NULL  
auto\_increment, `type` varchar(255) default NULL, PRIMARY KEY (`id`))  
ENGINE=InnoDB DEFAULT CHARSET=latin1;

---

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [March 8, 2006, 1:51am UTC](https://www.ruby-forum.com/t/sti-and-unit-testing/77966/2 "2006-03-08T01:51:05Z")

</div>

Why not just have both BarTest and FooTest extend Test::Unit::TestCase

class BarTest \< Test::Unit::TestCase  
…  
end

class FooTest \< Test::Unit::TestCase  
…  
end

That would seem to solve your problem.

-Jonny

---

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [March 8, 2006, 3:55am UTC](https://www.ruby-forum.com/t/sti-and-unit-testing/77966/3 "2006-03-08T03:55:04Z")

</div>

Alan B. wrote:

> Hi all, I’m having a bit of a problem with unit testing. I’ve simplified my  
> models to try and get my head round it but I’m still struggling. I’m using  
> STI and trying to write tests for the extended models (code below). I’ve  
> written test cases for the extended models which themselves extend the test  
> case for the parent model, so BarTest extends FooTest which extends TestCase
> 
> when I run ‘bar\_test.rb’ it seems to run the test case twice, once for  
> FooTest and once for BarTest, even though the only file I’m running is  
> bar\_test.rb - here’s the output:

My guess - and it’s only a guess - is that because BarTest requires  
FooTest to be loaded, and because FooTest appears to the test framework  
to be a valid test class, with a runnable test method, FooTest is  
getting run as well.

Justin

---

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [March 8, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/sti-and-unit-testing/77966/4 "2006-03-08T17:12:00Z")

</div>

“Jonathan V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
message news:removed\_email\_address@domain.invalid…

> That would seem to solve your problem.  
> wouldnt that mean having to write the same set of tests for both  
> classes?  
> BarTest should ideally test everything that FooTest does, and also any  
> custom tests required for class Bar
