LoginSignup
3
3

More than 5 years have passed since last update.

Plack::Middlewareに依存したアプリをTest::Mojoでテストする

Last updated at Posted at 2014-05-13

Plack::Middlewareに依存した動作をするアプリの場合、
特になにもしないとTest::Mojoでテストができないので、
以下の手順で対応した。

Mojolicious::Plugin::PlackMiddleware の導入

Mojoliciousのフックであるaround_dispatch内で、
Plack::Middlewareを使えるようにしてくれるプラグイン。

導入は普通に以下のようになる。

$ cpanm Mojolicious::Plugin::PlackMiddleware

Plack::Builderを使うのではなくて、
こちらのプラグインを使う方針であれば解決。

今回は、やはり起動スクリプト内でPlack::Builderを使いたかったので、
もう一手間行った。

Test::Mojoでビルドしたアプリにプラグイン設定する

以下のように、
Test::Mojoでいったんアプリを普通にビルドして、
そのアプリに対してプラグイン設定をする形をとった。

t/basic.t
use Mojo::Base -strict;

use Test::More;
use Test::Mojo;

my $t = Test::Mojo->new('MyApp');

$t->app->plugin(plack_middleware => [
    'Session' => {store => 'File'},
]);

$t->get_ok('/')->status_is(200)->content_like(qr/Mojolicious/i);

done_testing();

$t->appで、アプリを参照できるので、
pluginメソッドでプラグインを設定している。

参考

https://metacpan.org/pod/Mojolicious::Plugin::PlackMiddleware
https://metacpan.org/pod/HTTP::Server::PSGI

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3