Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created March 13, 2014 10:59
Show Gist options
  • Save memememomo/9526334 to your computer and use it in GitHub Desktop.
Save memememomo/9526334 to your computer and use it in GitHub Desktop.
Test::Mojoで、JSONを返すAPIをテストする。 ref: http://qiita.com/uchiko/items/70c132bab5e66783e474
use strict;
use warnings;
use utf8;
use Test::More;
use Test::Mojo;
use File::Basename;
$ENV{MOJO_HOME} = dirname(__FILE__) . '/../';
require "$ENV{MOJO_HOME}/myapp.pl";
my $t = Test::Mojo->new;
$t->get_ok('/case1')
->status_is(200)
->json_is({status => 'ok'});
$t->get_ok('/case2')
->json_is({
status => 'ok',
list => [
{
id => 1,
name => '太郎',
},
{
id => 2,
name => '花子',
},
{
id => 3,
name => '冴子',
}
]
});
$t->get_ok('/case1')
->status_is(200)
->json_is('/status' => 'ok');
$t->get_ok('/case2')
->status_is(200)
->json_has('/status')
->json_is('/status' => 'ok')
->json_is('/list/0/id' => 1)
->json_is('/list/0/name' => '太郎')
->json_is('/list/1/id' => 2)
->json_is('/list/1/name' => '花子')
->json_is('/list/2/id' => 3)
->json_is('/list/2/name' => '冴子');
done_testing;
use Mojolicious::Lite;
use utf8;
my %case = (
case1 => {
status => 'ok',
},
case2 => {
status => 'ok',
list => [
{
id => 1,
name => '太郎',
},
{
id => 2,
name => '花子',
},
{
id => 3,
name => '冴子',
}
],
},
);
get '/:case' => sub {
my $self = shift;
$self->render(
json => $case{$self->param('case')}
);
};
app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment