Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Laravel helper functions when unit testing

I am testing a class I made that uses a helper function. The function I'm testing looks like:

public function upload(UploadedFile $file)
{
    $file = $file->move(base_path() . '/temp');
    $file = new FileSystemPicture($file);

    return $file;
}

When I run the tests and it hits the helper function it says:

PHP Fatal error:  Call to a member function make() on a non-object

I have tracked the helper function down to be the following code:

function base_path($path = '')
{
    return app()->make('path.base').($path ? '/'.$path : $path);
}

I know that I'm getting the error because the "app" hasn't been created because I'm only testing the my class, not the whole application. Are there other effective ways to get the root directory of my project or is there a way to force the "app" to load so it can be used in this function?

like image 854
searsaw Avatar asked Nov 01 '13 12:11

searsaw


1 Answers

I had this problem, solved making sure that:

  1. My test class extended TestCase.
  2. If my test class had a setUp() method, it called parent::setUp()

app_path() and similar functions worked after that.

like image 187
Cmorales Avatar answered Sep 23 '22 04:09

Cmorales