Testing Hello World of Dancer

It is great that we can create a web page using Dancer, but if our application has any value to us then we will want to make sure it works as expected and that it continues to work even after some changes were made.

The fastest and cheapest way to do this to write tests. Unit-, integration-, acceptance tests, name them as you like, the important part is that they verify that the code works (and fails) as expected.

As this is such an integral part of writing code, we won't delay writing tests to the end of the project. We jump in right now.

Next to our app.psgi we create a file called test.t with the following content.

use strict;
use warnings;

use Test::More;
use Plack::Test;
use Plack::Util;
use HTTP::Request::Common;

my $app = Plack::Util::load_psgi './app.psgi';

my $test = Plack::Test->create($app);
my $res = $test->request(GET '/');

is $res->status_line, '200 OK', 'Status';
is $res->content, 'Hello World!', 'Content';

We can then run it by typing in prove -v test.t on the command line. this is going to be the output:

prove -v test.t

test.t .. 
ok 1 - Status
ok 2 - Content
All tests successful.
Files=1, Tests=2,  0 wallclock secs ( 0.02 usr  0.01 sys +  0.28 cusr  0.01 csys =  0.32 CPU)
Result: PASS