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