[dancer-users] Dancer2: Create a configuration for testing

Mikko Johannes Koivunalho mikko.koivunalho at iki.fi
Thu Jan 7 16:04:37 EST 2021


Hi.

I have a Dancer2 app.
I want to test it properly against a database.
For every test run I create a new database and a new empty schema
(PostgreSQL).
My new database has a random name (e.g. app_123abc) and at the end of
the test I drop the database.

How can I change the Dancer2 app configuration during the test run, or
before I do
my $test = Plack::Test->create( $app );
?

As it is, is just loads the config files for development. I want to
provide my own configuration programmatically. Creating files in the
current directory is not an option.

I have tried:
config->{'my_stuff'} = \%my_stuff
set 'my_stuff' => \%my_stuff

but Dancer2 doesn't seems to pick it up or it gets overwritten later by
the config from the files.

Example:

#!/perl
## no critic (Modules::ProhibitMultiplePackages)
use strict;
use warnings;
use 5.014; # JSON::PP included in CORE.

package ConfigChangeTest;
use Dancer2;
set serializer => 'JSON';
get qr{/info}msx => sub {
    return { appname => config->{'appname'}, };
};

package main;

use Test2::V0 -srand => time, qw(:DEFAULT);
use Plack::Test;

use HTTP::Request;
use JSON;
use Dancer2;

my $appname = 'ConfigChangeTest';
set appname => $appname;

my $test = Plack::Test->create( ConfigChangeTest->to_app );
my $req = HTTP::Request->new( GET => '/api/1/info' );
my $res = $test->request( $req );

ok( $res->is_success, 'Is success' );

# Fails!
# appname is the one set in the file `environments/development.yml` or
+ file `config.yml`.
is( decode_json $res->content, { appname => $appname, }, 'Is right con
+tent' );

done_testing;





-- 
Mikko Koivunalho
LinkedIn: http://www.linkedin.com/in/MikkoKoivunalho
AboutMe: http://about.me/mikkokoivunalho
Blog: http://www.koivunalho.org/blogs/exercises-in-integration-and-delivery/
[ This message has been signed with PGP. Public key attached. ]

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0xA672AD0F974B77B8.asc
Type: application/pgp-keys
Size: 18170 bytes
Desc: not available
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20210107/7aa22f07/attachment.key>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20210107/7aa22f07/attachment.sig>


More information about the dancer-users mailing list