Template testing
#2115
-
Hello, is there a way how to test the code embedded in html templates, i.e. just pass the needed variables and test the result? Best regards |
Beta Was this translation helpful? Give feedback.
Answered by
Akron
Sep 29, 2023
Replies: 1 comment 9 replies
-
Yes - and it's fairly simple. You can directly test templates using the #!/usr/bin/env perl
use Mojo::Template;
use Test::More;
my $mt = Mojo::Template->new;
my $template_rendering = $mt->vars(1)->render(<<'TEMP', {works => 'Also fun'});
<p><%= uc($works) %></p>
TEMP
is($template_rendering,"<p>ALSO FUN</p>\n");
done_testing; So simply render the template with the passed values and check the result. You can also check the code on a higher level (e.g. when not using #!/usr/bin/env perl
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;
helper print_uc => sub {
my ($c, $str) = @_;
return uc($str);
};
get '/' => 'fun';
get '/fine' => sub {
my $c = shift;
$c->stash(works => 'FUN');
$c->render('fun');
};
# Test the embedded code
my $t = Test::Mojo->new;
$t->get_ok('/')
->status_is(500)
;
$t->get_ok('/fine')
->status_is(200)
->text_is('p', 'FUN')
;
done_testing;
__DATA__
@@fun.html.ep
<p><%= print_uc($works) %></p> |
Beta Was this translation helpful? Give feedback.
9 replies
Answer selected by
dkrupicka
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes - and it's fairly simple. You can directly test templates using the
Mojo::Template
API like this:So simply render the template with the passed values and check the result.
You can also check the code on a higher level (e.g. when not using
Mojo::Template
as your template engine) by usingTest::Mojo
like this: