samedi 4 février 2017

Laravel - Unit test for Method

I still new in Laravel, and I have to write a test for a class which has this method:

private function generateImageUrl($files)
    {
        $timestamp = time();
        $exploded_file_name = explode(".", $files->getClientOriginalName());
        $filename = $timestamp . "." . $exploded_file_name["1"];
        $files->move(env('PICTURES_REPO'), $filename);
        $fileUrl = $filename;
        return $fileUrl;
    }

How can I cover this method by test ? I did this but it's not working :

public function testgenerateImageUrl()
    {
        $request = m::mock(Request::class, ['hasFile' => true]);

        $this->app->instance(Request::class, $request);

        $file = m::mock(UploadedFile::class, [
            'getClientOriginalName' => 'test.jpg'
        ]);

        $file->shouldReceive('move')
            ->once()
            ->with('PICTURES_REPO', 'test');

        $files = ['file' => $file];

        $this->call('POST', 'public/uploads', [], [], $files);

        $this->assertEquals(['file_name' => 'test'], json_decode($this->response->getContent(), true));
    }

Thanks in advance !



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jIkfoC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire