mikkoi has asked for the wisdom of the Perl Monks concerning the following question:
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_stuffset 'my_stuff' => \%my_stuff but Dancer2 doesn't seems to pick it up or it gets overwritten later by the config from the files.
Update:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dancer2: Create a configuration for testing
by kcott (Archbishop) on Jan 06, 2021 at 05:47 UTC | |
by 1nickt (Canon) on Jan 06, 2021 at 13:26 UTC | |
by kcott (Archbishop) on Jan 06, 2021 at 14:38 UTC | |
|
Re: Dancer2: Create a configuration for testing
by 1nickt (Canon) on Jan 05, 2021 at 23:54 UTC | |
by mikkoi (Beadle) on Jan 06, 2021 at 21:30 UTC | |
|
Re: Dancer2: Create a configuration for testing
by kcott (Archbishop) on Jan 06, 2021 at 13:23 UTC |