So simply render the template with the passed values and check the result.
\nYou can also check the code on a higher level (e.g. when not using Mojo::Template
as your template engine) by using Test::Mojo
like this:
#!/usr/bin/env perl\nuse Mojolicious::Lite;\nuse Test::Mojo;\nuse Test::More;\n\nhelper print_uc => sub {\n my ($c, $str) = @_;\n return uc($str);\n};\n\nget '/' => 'fun';\n\nget '/fine' => sub {\n my $c = shift;\n $c->stash(works => 'FUN');\n $c->render('fun');\n};\n\n# Test the embedded code\nmy $t = Test::Mojo->new;\n\n$t->get_ok('/')\n ->status_is(500)\n ;\n\n$t->get_ok('/fine')\n ->status_is(200)\n ->text_is('p', 'FUN')\n ;\n\ndone_testing;\n\n__DATA__\n@@fun.html.ep\n<p><%= print_uc($works) %></p>
-
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.
-
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.
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: