Created
September 30, 2010 14:25
-
-
Save marcusramberg/604658 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=encoding utf8 | |
=head1 NAME | |
Mojolicious::Guides::Rendering - Rendering | |
=head1 OVERVIEW | |
Testing your L<Mojolicious> application. | |
=head1 CONCEPTS | |
unit tests, client side code, integration tests | |
=head1 The Basics | |
=head2 Testing your Mojolicious Application | |
Rather than being just another buzz word, Test Driven Development is a radical | |
change in the way we develop software. In order to allow easy testing of your | |
Mojolicious application, we include Test::Mojo, a way to perform integration | |
tests directly against your application. This works quite well with the | |
standard Test::More suite used in most perl distributions: | |
use Test::More tests=>3; | |
use Mojo::Test; | |
my $t=Mojo::Test->new(app => 'MyApp'); | |
$t->get_ok('/') | |
->status_is(200) | |
->content_like(qr/Hello/); | |
Note that the test methods are chained for your convenience. If you need to | |
access the request and response objects for further testing,you can get to | |
them through $t->tx (short for transaction). Test::Mojo also supports a lot | |
of other ways to test your request. See the POD for L<Test::Mojo> for all | |
the methods you can use to test your application. | |
=head2 Testing your Mojolicious::Lite Application. | |
Since a Mojolicious::Lite application is a script rather than a package, we | |
need to do a little extra work to load it in our t/ files: | |
use FindBin; | |
$ENV{MOJO_HOME} = "$FindBin::Bin/../"; | |
require "$ENV{MOJO_HOME}/myapp.pl"; | |
my $t = Test::Mojo->new; | |
=head1 Testing the Parts | |
=head2 Using Mojo::DOM for client-testing | |
Recent versions of Mojo include Mojo::DOM, a simple CSS-selector based | |
liberal XML parser. In practice, it can even parse most HTML-documents, | |
making it ideal for testing the interface of your application, using | |
the same CSS selectors jQuery use to target your javascript actions. | |
The implementation is quite full featured, including pseudo operators | |
like ':checked' and ':empty', which can be quite useful when testing | |
your forms. You can even do things like ':checked[value="foo"]'. | |
Mojo::Test also includes an number of testing_methods which leverage | |
Mojo::DOM, including $t->element_exists, $t->text_is and $t->text_like | |
Here's a simple example: | |
$t->get_ok('/') | |
->element_exists('.header h3','Check existence of header title') | |
->text_is('.foo:checked'),'groovy','Check text for checked element') | |
=head1 Testing your Model | |
Of course, in every well structured application, most of your logic should | |
residen in the model layer. Mojolicious is model-agnostic to the point of | |
not even including Model-adapters like for instance Catalyst. This means | |
that you can test your data models completely separate from the rest of | |
your Mojolicious application. Just write unit tests as you would normally | |
do. | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment