Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify the name of a file?

I have a function that returns a file for the user avatar and I want to create tests for it. What I want to do is to check the name of the file.

This is the function:

sub get_avatar {
   my $self = shift;
   my $username = $self->stash('username');

   my $home = Mojo::Home->new;
   $home->detect('Project');
   my $path = $home->child('users', 'avatars', "$username");

   $path = $home->child('img', 'default.png') if !(-e $path);

   $self->render_file('filepath' => $path);
}

And this is the test:

$file = $t->get_ok('/user/username/avatar')->status_is(200);
  //use Data::Dumper;
  //diag Dumper($file);

ok $file =~ 'username';

I want to check if the name of the $file is equivalent to 'username'(which is the name of the file from the server) or 'default' if it is a default avatar.

like image 582
Marinescu Avatar asked Nov 06 '22 04:11

Marinescu


1 Answers

Don't work with the file path. Work with the actual image. Create two small test images. They can be 2x2 pixel PNG files. Maybe make them single colour, but different. Have one be your default one.

Then serialise that and put it in your test as a string. Run $t->tx->res->body through the same serialisation, and compare these two.

If you wanted, you could also make the test deploy that image before running the code, so your application doesn't depend on the image being there.

like image 156
simbabque Avatar answered Nov 15 '22 05:11

simbabque