Testing the current time with Dancer


Just like in the first case, we would like to make sure our code works now and that it keeps working after we make changes. So we are going to write a test for this application as well.

However unlike in the previous case, here we cannot compare the results to a fixed value as the result will be different every time we run the test.

We could mock the time generating code of the Dancer application, but for this application it would be an overkill.

So instead of that we weaken our test and compare the results to a regular expression. So we don't know that the returned string is indeed the correct date and time, but at least we can know that it looks like one.

The like keyword of Test::More provides this testing functionality.


examples/dancer/show_time/test.t
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';
like $res->content, qr/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/, 'Content';

done_testing();