STI followup: unit tests
I'm a complete newbie when it comes to unit testing in Ruby (I started only today), but here's a unit test for yesterday's STI example.
Test data (in test/fixtures/element_defs.yml
) below. Note that the first entry root_name_def
is subclassed to RefComponentDef
.
root_name_def:
type: RefComponentDef
id: 1
root_description_def:
id: 2
In my tests for the parent class ElementDef
it looks like I'm disregard subclassing altogether, but passing this test shows implicitly that subclasses and their objects load fine together:
class ElementDefTest < Test::Unit::TestCase
fixtures :element_defs
# Check that the fixture data makes sense
def test_fixtures
assert_kind_of ElementDef, element_defs(:root_name_def)
assert_kind_of ElementDef, element_defs(:root_description_def)
end
# Check that the loaded data makes sense
def test_loaded_data
assert_kind_of ElementDef, ElementDef.find_by_id(element_defs(:root_name_def)[:id])
assert_kind_of ElementDef, ElementDef.find_by_id(element_defs(:root_description_def)[:id])
end
In the subclass's tests I show that its objects can be read via the superclass's find methods:
I could add this to the wiki - did anyone find this useful?
class RefComponentDefTest < Test::Unit::TestCase
fixtures :element_defs
# Check the ElementDef fixture data correctly identifies :root_name_def as a RefComponentDef
def test_fixtures
assert_kind_of RefComponentDef, element_defs(:root_name_def)
end
# Check that a RefComponentDef can be read back from the database via its superclass
def test_loaded_data
assert_kind_of RefComponentDef, ElementDef.find_by_id(element_defs(:root_name_def)[:id])
end
end
Technorati tag: rails
0 Comments:
Post a Comment
<< Home