[Catalyst] Modify config in a test

Vladimir Timofeev vovkasm at gmail.com
Wed Sep 26 05:56:23 GMT 2012


2012/9/26 Bill Moseley <moseley at hank.org>:
> I'm using Catalyst::Test on an app.
>
> The app has a myapp.yml config which includes configuration for creating an
> instance of a Model component -- and part of that Model's config is a
> database dsn attribute.   When running my test I do want to have the app
> load this config file -- but I want to modify it on-the-fly.
>
> My test builds a SQLite database when it runs, and I'd like to be able to
> modify the config as (before) Catalyst creates the instance of the model
> component -- that is just filter the config to inject my temporary database.
>
> The model "dsn" attribute is readonly -- otherwise I could just do
> MyApp->model( 'MyModel' )->dns( $new_dsn );
>
> What's a good, clean way of doing this?   Or perhaps a better approach all
> together?

Ohh.. I'am also interested in "clean" way to do this.
But now in the similar situation, I'm doing this:
(it is from my base test class whitch extends Test::Class itself)

use Class::Load qw(load_class is_class_loaded);
use Class::Method::Modifiers ();
...
sub default_config { return {
    ... # somewere dsn => 'dbi:SQLite::memory:'
  };
}

sub init : Test(startup) {
  my $self = shift;
  my $app_class = 'MyApp';
  ...
  unless (is_class_loaded($app_class)) {
    load_class($app_class);
    $app_class->config($self->default_config); # set default config

    # filter out some plugins
    Class::Method::Modifiers::install_modifier('Catalyst', 'around',
'setup_plugins', sub {
      my $orig = shift;
      my ($class, $plugins) = @_;
      $orig->($class, [grep { ! ( $_ ~~ ['ConfigLoader',<some other
plugins>] ) } @$plugins]);
    });
    $app_class->import; # now allow Catalyst properly setup application
    # create schema
    $app_class->model('General')->schema->deploy({quote_field_names=>1,quote_table_names=>1});
  }
  ...
}

Hackish, but works...

>
> --
> Bill Moseley
> moseley at hank.org
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>



-- 
Vladimir Timofeev <vovkasm at gmail.com>



More information about the Catalyst mailing list