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