| 1 |
package HTML::FormHandler::Manual::Testing |
| 2 |
# ABSTRACT: testing forms |
| 3 |
|
| 4 |
__END__ |
| 5 |
|
| 6 |
=pod |
| 7 |
|
| 8 |
=encoding UTF-8 |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
HTML::FormHandler::Manual::Testing - testing forms |
| 13 |
|
| 14 |
=head1 VERSION |
| 15 |
|
| 16 |
version 0.410002 |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
L<Manual Index|HTML::FormHandler::Manual> |
| 21 |
|
| 22 |
One of the big advantages of FormHandler compared to many other form |
| 23 |
packages is that you can test the same form that you use in your |
| 24 |
controller. |
| 25 |
|
| 26 |
=head1 DESCRIPTION |
| 27 |
|
| 28 |
It's difficult to test forms that are instantiated in controllers with 'add_element' |
| 29 |
calls 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 |
| 31 |
anything except the simplest forms. If you have a form class that contains everything |
| 32 |
that is needed for processing the form, it's really really easy to create tests for |
| 33 |
forms. Look in the FormHandler 't' directory. It's full of tests for forms. |
| 34 |
|
| 35 |
You can test that the validations work, that the database is getting updated |
| 36 |
correctly, even that the HTML that's being rendered is correct. If something |
| 37 |
isn't working correctly, it's ten times easier to debug in a test case than |
| 38 |
sitting in a controller somewhere. And when you finally start up your application |
| 39 |
and use the form, there should be very few surprises. |
| 40 |
|
| 41 |
FormHandler provides a simple function to test whether the HTML output is |
| 42 |
correct, 'is_html' in L<HTML::FormHandler::Test>, which uses L<HTML::TreeBuilder>. |
| 43 |
If you need to build forms that use the rendering code to produce particular |
| 44 |
output, it can be helpful. |
| 45 |
|
| 46 |
=head1 Example |
| 47 |
|
| 48 |
Here's an example of a test, originally copied from one of the DBIC model tests. |
| 49 |
But you should download the tar.gz or checkout the distribution from github |
| 50 |
and 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 |
|
| 113 |
FormHandler Contributors - see HTML::FormHandler |
| 114 |
|
| 115 |
=head1 COPYRIGHT AND LICENSE |
| 116 |
|
| 117 |
This software is copyright (c) 2017 by Gerda Shank. |
| 118 |
|
| 119 |
This is free software; you can redistribute it and/or modify it under |
| 120 |
the same terms as the Perl 5 programming language system itself. |
| 121 |
|
| 122 |
=cut |