Skip to content

Template testing #2115

Answered by Akron
dkrupicka asked this question in Q&A
Sep 27, 2023 · 1 comments · 9 replies
Discussion options

You must be logged in to vote

Yes - and it's fairly simple. You can directly test templates using the Mojo::Template API like this:

#!/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 Mojo::Template as your template engine) by using Test::Mojo like this:

#!/usr/bin/env perl
use Mojolicious::Lite;
use Test::Mojo;
use Test::More;

helper print_uc => sub {
  my ($c, $str) = @_;
  r…

Replies: 1 comment 9 replies

Comment options

You must be logged in to vote
9 replies
@s1037989
Comment options

@dkrupicka
Comment options

@dkrupicka
Comment options

@ernstki
Comment options

@ernstki
Comment options

Answer selected by dkrupicka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants