Skip to content

Instantly share code, notes, and snippets.

@kwakwaversal
Last active September 18, 2017 14:27
Show Gist options
  • Save kwakwaversal/8555320 to your computer and use it in GitHub Desktop.
Save kwakwaversal/8555320 to your computer and use it in GitHub Desktop.
How to test a standalone mojolicious app. #mojo #perl
package ExceptionTestController;
use Mojo::Base 'Mojolicious::Controller';
use Data::Validator::Results::Any;
sub raise_known_exception {
my $self = shift;
return unless $self->rest_eval(
sub {
Data::Validator::Results::Any->throw(
{field => 'forename', code => 'not_blank',});
}
);
$self->render(json => {ok => '1'});
}
sub raise_die_exception {
my $self = shift;
die "what?";
}
1;
package ExceptionTest;
use Mojo::Base 'Mojolicious';
sub startup {
my $self = shift;
my $r = $self->routes;
$self->plugin('REST::Response');
$self->plugin('JSON');
# raise_exception
$r->route('/:action')
->to(namespace => 'ExceptionTestController');
}
1;
package main;
use Mojo::Base -strict;
BEGIN {
$ENV{MOJO_LOG_LEVEL} = 'error';
}
use Test::More;
use FindBin;
use lib "$FindBin::Bin/lib";
use Test::Mojo;
use Data::Dumper;
my $t = Test::Mojo->new('ExceptionTest');
$t->get_ok('/raise_known_exception.json')->status_is(400)
->json_is('/errors/0/code' => 'field_missing');
$t->get_ok('/raise_die_exception.json')->status_is(500)->content_like(qr/what?/);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment