Skip to content
\n

So simply render the template with the passed values and check the result.

\n

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:

\n
#!/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>
","upvoteCount":2,"url":"https://github.com/mojolicious/mojo/discussions/2115#discussioncomment-7143206"}}}

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