0
我試圖使用Laravel和PhpUnit測試圖片上傳。在測試課外,它工作正常。但是,當我運行測試,我發現了以下錯誤:使用phpunit和嘲諷進行圖片上傳測試
1) App\UploadTest::it_uploads_an_image_on_post Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_Symfony_Component_HttpFoundation_File_UploadedFile::move("posts/photos", "/nowfoo.jpg"). Either the method was unexpected or its argumen ts matched no expected argument list for this method
和我的測試文件:
<?php
namespace App;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Mockery as m;
use App\BBImage;
class UploadTest extends \TestCase
{
/**
* @test
*/
public function it_uploads_an_image_on_post()
{
$file = m::mock(UploadedFile::class, [
'getClientOriginalName' => 'foo',
'getClientOriginalExtension' => 'jpg'
]);
$file->shouldReceive('move')
->once()
->with('posts/photos', 'nowfoo.jpg');
$photo = new BBImage($file);
$this->assertEquals('posts/photos/nowfoo.jpg', $photo->makePhoto());
}
}
function time() { return 'now'; }
function sha1($path) {return $path;}
和我BBImage.php類
<?php
namespace App;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class BBImage
{
protected $file;
public function __construct(UploadedFile $file)
{
$this->file = $file;
}
public function makePhoto()
{
$path = 'posts/photos';
$name = $this->makeFileName();
$this->file->move($path, $name);
return ('/'.$path.$name);
}
protected function makeFileName()
{
$name = sha1(
time() . $this->file->getClientOriginalName()
);
$extension = $this->file->getClientOriginalExtension();
return "/$name.$extension";
}
}