Testing internal method

31 views
Skip to first unread message

Alberto Mijares

unread,
Apr 27, 2020, 6:55:43 PM4/27/20
to Mojo Users Group
Hi guys,

I've been reading about testing and can't figure out how to test
internal methods; meaning those methods not directly associated to a
route.

I'm sure most of you know what I'm talking about but just for the
record. Let's say you have in App::Controller::This

sub list {
my $self = shift;
$self->render(text => $self->filter($value));
}

sub filter {
my $self = shift;
for @list {push (@final, $line) if ($line =~ /something/);}
return \@final
}

And in lib/App.pm you have

$c->get('/list)->to('this#list');

So, I want to test returned values from App::Controller::This->filter.

Thanks in advance for your help.


Alberto Mijares

Scott Wiersdorf

unread,
Apr 27, 2020, 7:09:09 PM4/27/20
to Mojolicious
Test::Mojo can help you test your views and controllers, as well as your internal helpers:


If you are wanting to test your application model (i.e., non-Mojolicious code), you should probably use standard Test::More to create an object, exercise its methods, etc.

Scott

Alberto Mijares

unread,
Apr 27, 2020, 11:32:44 PM4/27/20
to Mojo Users Group


On Mon, Apr 27, 2020, 7:09 PM Scott Wiersdorf <scott.w...@gmail.com> wrote:
Test::Mojo can help you test your views and controllers, as well as your internal helpers:


Thank you Scott.

I did read the documentation before writing this email and saw what you are pointing out. The thing is: it's not clear enough for me.

Given the example in my original post, could you give me a more detailed explanation? I don't know how to trigger this specific method.

Glen

unread,
Apr 27, 2020, 11:46:29 PM4/27/20
to mojol...@googlegroups.com
He's implying that you wouldn't test this internal method with Test::Mojo; Test::Mojo is for testing endpoints.

If you want to test a specific method that is not an endpoint, load the module manually and test it with Test::More.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CAGZBXN9VBvBFZdkmpaJOPHFJ5V5h98GdqmsqjH%2B7wN6bRNpAuQ%40mail.gmail.com.

Stefan Adams

unread,
Apr 28, 2020, 12:41:06 AM4/28/20
to mojolicious
I think Scott and Glen are both pointing out that your method in your controller class that is not associated with a route ("filter") is not a method of your controller, but instead in the "model" portion of MVC.  It might help you to move that code outside of the controller class, and then it'll make much more sense to test it normally with Test::More instead of Test::Mojo.

# lib/App/Controller/This.pm
package App::Controller::This;
use Mojo::Base 'Mojolicious::Controller';

sub list {
  my $self = shift;
  my $model = App::Model->new;
  my $value = $self->req->json('/value');
  $self->render(json => {value => $model->filter(@$value)});
}

# lib/App/Model.pm
package App::Model;
use Mojo::Base -base;
sub filter {
  my $self = shift;
  my $final;
  /something/ and push @$final, $_ for @_;
  return $final;
}

And then you can test your filter method normally:

# t/basic.t
use Test::More;
use Test::Mojo;

# Test the controller / action and the use of any models
my $t = Test::Mojo->new('App');
$t->get_ok('/list' => json => {value => ['abc', 'def something ghi', 'jkl', 'mno something pqr']})
   ->status_is(200)
   ->json_is('/value', ['def something ghi', 'mno something pqr']);

# Test just the model
my $model = App::Model->new;
isa_ok $model, 'App::Model';
is_deeply $model->filter('abc', 'def something ghi', 'jkl', 'mno something pqr'), ['def something ghi', 'mno something pqr'];

Alberto Mijares

unread,
Apr 28, 2020, 2:04:47 AM4/28/20
to Mojo Users Group
On Mon, Apr 27, 2020 at 11:46 PM Glen <gl...@empireenterprises.com> wrote:
>
> He's implying that you wouldn't test this internal method with Test::Mojo; Test::Mojo is for testing endpoints.


Thank you, guys.

This was the explanation I was needing.

Tests running now.


Alberto Mijares
Reply all
Reply to author
Forward
0 new messages