1HTML::FormHandler::ManuUasle:r:TCeosnttirnigb(u3t)ed PerHlTMDLo:c:uFmoernmtHaatnidolner::Manual::Testing(3)
2
3
4
6 HTML::FormHandler::Manual::Testing - testing forms
7
9 version 0.40068
10
12 Manual Index
13
14 One of the big advantages of FormHandler compared to many other form
15 packages is that you can test the same form that you use in your
16 controller.
17
19 It's difficult to test forms that are instantiated in controllers with
20 'add_element' calls and from YAML, and that have no form class. It's
21 one of the reasons that 'dynamic' forms generated with a field_list
22 aren't a good idea for anything except the simplest forms. If you have
23 a form class that contains everything that is needed for processing the
24 form, it's really really easy to create tests for forms. Look in the
25 FormHandler 't' directory. It's full of tests for forms.
26
27 You can test that the validations work, that the database is getting
28 updated correctly, even that the HTML that's being rendered is correct.
29 If something isn't working correctly, it's ten times easier to debug in
30 a test case than sitting in a controller somewhere. And when you
31 finally start up your application and use the form, there should be
32 very few surprises.
33
34 FormHandler provides a simple function to test whether the HTML output
35 is correct, 'is_html' in HTML::FormHandler::Test, which uses
36 HTML::TreeBuilder. If you need to build forms that use the rendering
37 code to produce particular output, it can be helpful.
38
40 Here's an example of a test, originally copied from one of the DBIC
41 model tests. But you should download the tar.gz or checkout the
42 distribution from github and browse through the tests.
43
44 use Test::More;
45 use lib 't/lib';
46
47 use_ok( 'BookDB::Form::Book');
48 use_ok( 'BookDB::Schema::DB');
49
50 my $schema = BookDB::Schema::DB->connect('dbi:SQLite:t/db/book.db');
51 ok($schema, 'get db schema');
52
53 my $form = BookDB::Form::Book->new(schema => $schema);
54
55 # This is munging up the equivalent of param data from a form
56 my $good = {
57 'title' => 'How to Test Perl Form Processors',
58 'author' => 'I.M. Author',
59 'genres' => [2, 4],
60 'format' => 2,
61 'isbn' => '123-02345-0502-2' ,
62 'publisher' => 'EreWhon Publishing',
63 };
64 ok( $form->process( params => $good ), 'Good data' );
65
66 my $book = $form->item;
67 END { $book->delete };
68 ok ($book, 'get book object from form');
69 my $num_genres = $book->genres->count;
70 is( $num_genres, 2, 'multiple select list updated ok');
71 is( $form->field('format')->value, 2, 'get value for format' );
72
73 my $bad_1 = {
74 notitle => 'not req',
75 silly_field => 4,
76 };
77 ok( !$form->process( $bad_1 ), 'bad 1' );
78
79 my $bad_2 = {
80 'title' => "Another Silly Test Book",
81 'author' => "C. Foolish",
82 'year' => '1590',
83 'pages' => 'too few',
84 'format' => '22',
85 };
86 ok( !$form->process( $bad_2 ), 'bad 2');
87 ok( $form->field('year')->has_errors, 'year has error' );
88 ok( $form->field('pages')->has_errors, 'pages has error' );
89 ok( !$form->field('author')->has_errors, 'author has no error' );
90 ok( $form->field('format')->has_errors, 'format has error' );
91
92 my $good = {
93 title => "Another Silly Test Book",
94 author => "C. Foolish",
95 year => 1999,
96 pages => 101,
97 format => 2
98 };
99 ok( $form->process($good), 'now form validates' );
100
101 done_testing;
102
104 FormHandler Contributors - see HTML::FormHandler
105
107 This software is copyright (c) 2017 by Gerda Shank.
108
109 This is free software; you can redistribute it and/or modify it under
110 the same terms as the Perl 5 programming language system itself.
111
112
113
114perl v5.36.0 2022-07-H2T2ML::FormHandler::Manual::Testing(3)