1Test::Object(3) User Contributed Perl Documentation Test::Object(3)
2
3
4
6 Test::Object - Thoroughly testing objects via registered handlers
7
9 ###################################################################
10 # In your test module, register test handlers again class names #
11 ###################################################################
12
13 package My::ModuleTester;
14
15 use Test::More;
16 use Test::Object;
17
18 # Foo::Bar is a subclass of Foo
19 Test::Object->register(
20 class => 'Foo',
21 tests => 5,
22 code => \&foo_ok,
23 );
24 Test::Object->register(
25 class => 'Foo::Bar',
26 # No fixed number of tests
27 code => \&foobar_ok,
28 );
29
30 sub foo_ok {
31 my $object = shift;
32 ok( $object->foo, '->foo returns true' );
33 }
34
35 sub foobar_ok {
36 my $object = shift;
37 is( $object->foo, 'bar', '->foo returns "bar"' );
38 }
39
40 1;
41
42 ###################################################################
43 # In test script, test object against all registered classes #
44 ###################################################################
45
46 #!/usr/bin/perl -w
47
48 use Test::More 'no_plan';
49 use Test::Object;
50 use My::ModuleTester;
51
52 my $object = Foo::Bar->new;
53 isa_ok( $object, 'Foo::Bar' );
54 object_ok( $object );
55
57 In situations where you have deep trees of classes, there is a common
58 situation in which you test a module 4 or 5 subclasses down, which
59 should follow the correct behaviour of not just the subclass, but of
60 all the parent classes.
61
62 This should be done to ensure that the implementation of a subclass has
63 not somehow "broken" the object's behaviour in a more general sense.
64
65 "Test::Object" is a testing package designed to allow you to easily
66 test what you believe is a valid object against the expected behaviour
67 of all of the classes in its inheritance tree in one single call.
68
69 To do this, you "register" tests (in the form of CODE or function ref‐
70 erences) with "Test::Object", with each test associated with a particu‐
71 lar class.
72
73 When you call "object_ok" in your test script, "Test::Object" will
74 check the object against all registered tests. For each class that your
75 object responds to "$object->isa($class)" for, the appropriate testing
76 function will be called.
77
78 Doing it this way allows adapter objects and other things that respond
79 to "isa" differently that the default to still be tested against the
80 classes that it is advertising itself as correctly.
81
82 This also means that more than one test might be "counted" for each
83 call to "object_ok". You should account for this correctly in your
84 expected test count.
85
87 Bugs should be submitted via the CPAN bug tracker, located at
88
89 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Object>
90
91 For other issues, contact the author.
92
94 Adam Kennedy <cpan@ali.as>
95
97 <http://ali.as/>, Test::More, Test::Builder::Tester, Test::Class
98
100 Copyright 2005, 2006 Adam Kennedy. All rights reserved.
101
102 This program is free software; you can redistribute it and/or modify it
103 under the same terms as Perl itself.
104
105 The full text of the license can be found in the LICENSE file included
106 with this module.
107
108
109
110perl v5.8.8 2006-09-06 Test::Object(3)