Discussion:
Configuration override via Test::Mojo not being used
(too old to reply)
Scott Wiersdorf
2018-09-27 14:48:06 UTC
Permalink
I have a package Foo that implements a Mojolicious::Lite application. It
has some default configuration. I instantiate this app via Test::Mojo and
attempt to override the configuration, but the old (default) value is
always used instead:


use Mojo::Base -strict;
use Test::More;
use Test::Mojo;

package Foo;
use Mojolicious::Lite -signatures;
plugin Config => {default => {jack => 'squat'}};
get '/' => sub ($c) { $c->render(text => $c->app->config->{jack}) };

package main;
my $t = Test::Mojo->new(Foo => {jack => 'flack'});
$t->get_ok('/')
->content_is('flack')
->or(sub { say STDERR $t->app->dumper(shift->tx->res->body) });

done_testing();


I've tried this same technique putting the Foo class in a separate file,
putting the configuration into a file (i.e., not using the `default`
attribute for Config), etc. and it always comes down to the override
configuration isn't being used by the app. Anyone have any ideas?

Scott
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Sebastian Riedel
2018-09-27 14:52:42 UTC
Permalink
Post by Scott Wiersdorf
Anyone have any ideas?
Upgrade Mojolicious?
--
Sebastian Riedel
https://mojolicious.org
https://github.com/kraih
https://twitter.com/kraih
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Scott Wiersdorf
2018-09-27 15:14:30 UTC
Permalink
I should have added that I’m running 8.01.
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Илья Рассадин
2018-09-27 15:31:06 UTC
Permalink
Config overriding works only for full apps

#! /usr/bin/env perl use Mojo::Base-strict; use Test::More; use Test::Mojo; package Foo; use Mojo::Base'Mojolicious', -signatures; sub startup ($self) {
$self->plugin(Config => {default => {jack => 'squat'}}); $self->routes->get('/')->to('main#index'); }

package Foo::Controller::Main; use Mojo::Base'Mojolicious::Controller', -signatures; sub index ($c) {$c->render(text => $c->app->config->{jack}) }

package main; my $t = Test::Mojo->new(Foo => {jack => 'flack'}); $t->get_ok('/')
->content_is('flack')
->or(sub {say STDERR$t->app->dumper(shift->tx->res->body) }); done_testing();
Post by Scott Wiersdorf
I should have added that I’m running 8.01.
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Sebastian Riedel
2018-09-27 16:00:47 UTC
Permalink
Post by Илья Рассадин
Config overriding works only for full apps
Actually that's not true, but i see this here is a special case.
Config overrides only
work with full apps and instances of lite apps. Lite apps in classes
are a pretty
bad hack and do not work here.
--
Sebastian Riedel
https://mojolicious.org
https://github.com/kraih
https://twitter.com/kraih
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Scott Wiersdorf
2018-09-28 03:49:41 UTC
Permalink
For anyone looking for a working example of mocking a service in a test
file and overriding the configuration (standard style disclaimers apply),
here is the equivalent of the original (non-working) post using a full
Mojolicious app:

use Mojo::Base -strict;
use Test::More;
use Test::Mojo;

package Foo;
use Mojo::Base 'Mojolicious';
sub startup {
my $app = shift;

$app->plugin(Config => {default => {jack => 'squat'}});

$app->routes->get('/' => sub {
my $c = shift;
$c->render(text => $c->app->config->{jack});
});
}


package main;
my $t = Test::Mojo->new(Foo => {jack => 'flack'});
$t->get_ok('/')
->content_is('flack')
->or(sub { say STDERR $t->app->dumper(shift->tx->res->body) });

done_testing();


In my case, I have a second lite app I've loaded in the test file that I
can now point to use the mocked instance—handy to have one test file stand
up and tear down the mocked service along with the tests.

Scott
Post by Sebastian Riedel
Post by Илья Рассадин
Config overriding works only for full apps
Actually that's not true, but i see this here is a special case.
Config overrides only
work with full apps and instances of lite apps. Lite apps in classes
are a pretty
bad hack and do not work here.
--
Sebastian Riedel
https://mojolicious.org
https://github.com/kraih
https://twitter.com/kraih
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...