.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "HTML::FormHandler::Manual::Testing 3pm" .TH HTML::FormHandler::Manual::Testing 3pm "2017-11-11" "perl v5.26.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTML::FormHandler::Manual::Testing \- testing forms .SH "VERSION" .IX Header "VERSION" version 0.40068 .SH "SYNOPSIS" .IX Header "SYNOPSIS" Manual Index .PP One of the big advantages of FormHandler compared to many other form packages is that you can test the same form that you use in your controller. .SH "DESCRIPTION" .IX Header "DESCRIPTION" It's difficult to test forms that are instantiated in controllers with 'add_element' calls and from \s-1YAML,\s0 and that have no form class. It's one of the reasons that \&'dynamic' forms generated with a field_list aren't a good idea for anything except the simplest forms. If you have a form class that contains everything that is needed for processing the form, it's really really easy to create tests for forms. Look in the FormHandler 't' directory. It's full of tests for forms. .PP You can test that the validations work, that the database is getting updated correctly, even that the \s-1HTML\s0 that's being rendered is correct. If something isn't working correctly, it's ten times easier to debug in a test case than sitting in a controller somewhere. And when you finally start up your application and use the form, there should be very few surprises. .PP FormHandler provides a simple function to test whether the \s-1HTML\s0 output is correct, 'is_html' in HTML::FormHandler::Test, which uses HTML::TreeBuilder. If you need to build forms that use the rendering code to produce particular output, it can be helpful. .SH "Example" .IX Header "Example" Here's an example of a test, originally copied from one of the \s-1DBIC\s0 model tests. But you should download the tar.gz or checkout the distribution from github and browse through the tests. .PP .Vb 2 \& use Test::More; \& use lib \*(Aqt/lib\*(Aq; \& \& use_ok( \*(AqBookDB::Form::Book\*(Aq); \& use_ok( \*(AqBookDB::Schema::DB\*(Aq); \& \& my $schema = BookDB::Schema::DB\->connect(\*(Aqdbi:SQLite:t/db/book.db\*(Aq); \& ok($schema, \*(Aqget db schema\*(Aq); \& \& my $form = BookDB::Form::Book\->new(schema => $schema); \& \& # This is munging up the equivalent of param data from a form \& my $good = { \& \*(Aqtitle\*(Aq => \*(AqHow to Test Perl Form Processors\*(Aq, \& \*(Aqauthor\*(Aq => \*(AqI.M. Author\*(Aq, \& \*(Aqgenres\*(Aq => [2, 4], \& \*(Aqformat\*(Aq => 2, \& \*(Aqisbn\*(Aq => \*(Aq123\-02345\-0502\-2\*(Aq , \& \*(Aqpublisher\*(Aq => \*(AqEreWhon Publishing\*(Aq, \& }; \& ok( $form\->process( params => $good ), \*(AqGood data\*(Aq ); \& \& my $book = $form\->item; \& END { $book\->delete }; \& ok ($book, \*(Aqget book object from form\*(Aq); \& my $num_genres = $book\->genres\->count; \& is( $num_genres, 2, \*(Aqmultiple select list updated ok\*(Aq); \& is( $form\->field(\*(Aqformat\*(Aq)\->value, 2, \*(Aqget value for format\*(Aq ); \& \& my $bad_1 = { \& notitle => \*(Aqnot req\*(Aq, \& silly_field => 4, \& }; \& ok( !$form\->process( $bad_1 ), \*(Aqbad 1\*(Aq ); \& \& my $bad_2 = { \& \*(Aqtitle\*(Aq => "Another Silly Test Book", \& \*(Aqauthor\*(Aq => "C. Foolish", \& \*(Aqyear\*(Aq => \*(Aq1590\*(Aq, \& \*(Aqpages\*(Aq => \*(Aqtoo few\*(Aq, \& \*(Aqformat\*(Aq => \*(Aq22\*(Aq, \& }; \& ok( !$form\->process( $bad_2 ), \*(Aqbad 2\*(Aq); \& ok( $form\->field(\*(Aqyear\*(Aq)\->has_errors, \*(Aqyear has error\*(Aq ); \& ok( $form\->field(\*(Aqpages\*(Aq)\->has_errors, \*(Aqpages has error\*(Aq ); \& ok( !$form\->field(\*(Aqauthor\*(Aq)\->has_errors, \*(Aqauthor has no error\*(Aq ); \& ok( $form\->field(\*(Aqformat\*(Aq)\->has_errors, \*(Aqformat has error\*(Aq ); \& \& my $good = { \& title => "Another Silly Test Book", \& author => "C. Foolish", \& year => 1999, \& pages => 101, \& format => 2 \& }; \& ok( $form\->process($good), \*(Aqnow form validates\*(Aq ); \& \& done_testing; .Ve .SH "AUTHOR" .IX Header "AUTHOR" FormHandler Contributors \- see HTML::FormHandler .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2017 by Gerda Shank. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.