Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:40:16+00:00 2026-05-23T12:40:16+00:00

We have created the following simple Mojolicious controller: package SampleApp::Pages; # $Id$ use strict;

  • 0

We have created the following simple Mojolicious controller:

package SampleApp::Pages;

# $Id$

use strict;
use warnings;

our $VERSION = '0.01';

use Mojo::Base 'Mojolicious::Controller';

sub home {
    my $self = shift;

    $self->render( 'title' => 'Home' );

    return;
}

sub contact {
    my $self = shift;

    $self->render( 'title' => 'Contact' );

    return;
}

sub about {
    my $self = shift;

    $self->render( 'title' => 'About' );

    return;
}

1;

The corresponding unit tests look as follows:

package Test::SampleApp::Pages;

# $Id$

use strict;
use warnings;

our $VERSION = '0.01';

use Carp;
use English '-no_match_vars';
use Readonly;
use Test::Mojo;
use Test::Most;

use base 'Test::Class';

Readonly my $SERVER_OK => 200;

sub startup : Tests(startup) {
    eval {
        require SampleApp;

        SampleApp->import;

        1;
    } or Carp::croak($EVAL_ERROR);

    return;
}

sub get_home : Tests(4) {
    my $test = shift;
    my $mojo = $test->mojo;

    $mojo->get_ok('/pages/home')->status_is($SERVER_OK);

    $mojo->text_is(
        'title',
        $test->base_title . ' | Home',
        '... and should have the right title'
    );

    $mojo->content_like(
        qr/<body>(?:\s*\S+\s*)+<\/body>/msx,
        '... and should have a non-blank body'
    );

    return;
}

sub get_contact : Tests(3) {
    my $test = shift;
    my $mojo = $test->mojo;

    $mojo->get_ok('/pages/contact')->status_is($SERVER_OK);

    $mojo->text_is(
        'title',
        $test->base_title . ' | Contact',
        '... and should have the right title'
    );

    return;
}

sub get_about : Tests(3) {
    my $test = shift;
    my $mojo = $test->mojo;

    $mojo->get_ok('/pages/about')->status_is($SERVER_OK);

    $mojo->text_is(
        'title',
        $test->base_title . ' | About',
        '... and should have the right title'
    );

    return;
}

sub base_title {
    my ( $self, $base_title ) = @_;

    if ( defined $base_title ) {
        $self->{base_title} = $base_title;
    }

    return $self->{base_title};
}

sub mojo {
    my ( $self, $mojo ) = @_;

    if ( defined $mojo ) {
        $self->{mojo} = $mojo;
    }

    return $self->{mojo};
}

sub setup : Tests(setup) {
    my $test = shift;

    $test->base_title('Mojolicious Sample App');

    $test->mojo( Test::Mojo->new( app => 'SampleApp', max_redirects => 1 ) );

    return;
}

1;

To us, this is more like functionality testing rather than unit testing

Is there a way to call the home method of the controller and test its output that doesn’t require starting up a server instance via Test::Mojo?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:40:17+00:00Added an answer on May 23, 2026 at 12:40 pm

    To test your controller’s wiring, use code such as the following.

    We begin t/pages.t with familiar front matter.

    use Mojolicious;
    use Test::More;
    

    Now create a testing subclass of SampleApp::Pages that records calls to render.

    package TestingPages;
    use Mojo::Base 'SampleApp::Pages';
    
    has 'render_called';
    has 'render_arg';
    
    sub render {
      my($self,%arg) = @_;
      $self->render_called(1);
      $self->render_arg({ %arg });
    }
    

    Your question used Test::Class, so continue with that theme.

    package Test::SampleApp::Pages;
    
    use base 'Test::Class';
    use Test::More;
    

    Note that die with no arguments propagates the most recent exception, so you don’t have to write $@ explicitly.

    sub startup : Test(startup) {
      eval { require SampleApp::Pages; SampleApp::Pages->import; 1 } or die;
    }
    

    In setup, instantiate the testing subclass, connect it to a Mojolicious instance, and turn off logging.

    sub setup : Test(setup) {
      my($self) = @_;
    
      my $c = TestingPages->new(app => Mojolicious->new);
      $c->app->log->path(undef);
      $c->app->log->level('fatal');
      $self->{controller} = $c;
    }
    

    In the home test, call the controller’s home method and inspect the results.

    sub home : Tests(2) {
      my($self) = @_;
      my $c = $self->{controller};
      $c->home;
      is $c->render_called, 1, "render called";
      is $c->render_arg->{title}, "Home", "correct title arg";
    }
    

    Finally, run your tests.

    package main;
    Test::SampleApp::Pages->runtests;
    

    Output:

    $ ./sampleapp.pl test
    Running tests from '/tmp/sampleapp/t'.
    t/pages.t .. ok   
    All tests successful.
    Files=1, Tests=2,  1 wallclock secs ( 0.03 usr  0.02 sys +  0.24 cusr  0.03 csys =  0.32 CPU)
    Result: PASS

    Now that you see how to do it, the question is whether it’s worth all the trouble. Controllers should be simple wiring. Consider whether any complexity in a controller really belongs in a model where testing is much more straightforward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created simple function create function TRIM(@data varchar(20)) returns varchar(100) as begin declare
I have created simple .aspx page which queries a database for some live data
I have created a simple WordPress page template. I am working on live server.
I have created a simple RAII class in one of my DLLs (let's call
I have created a simple app which shows a Google Map. I am testing
I am working on sharepoint 2010. I have created a simple visual web part
I have a snippet to create a 'Like' button for our news site: <iframe
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.