STI and unit testing

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):
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;

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

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

“Jonathan V.” [email protected] wrote in
message news:[email protected]

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