我尝试使用Laravel和PhpUnit测试图片上传。在考试课外,它工作正常。但是当我运行测试时,我收到以下错误:
1)App \ UploadTest :: it_uploads_an_image_on_post Mockery \ Exception \ NoMatchingExpectationException:没有匹配的处理程序 找到了 Mockery_0_Symfony_Component_HttpFoundation_File_UploadedFile ::移动("文章/照片&#34 ;, " /nowfoo.jpg")。无论是方法是出乎意料还是其论证 匹配此方法没有预期的参数列表
我的测试文件是:
<?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";
}
}
答案 0 :(得分:0)
我认为问题只是一个错字。你应该改变期望:
$file->shouldReceive('move')
->once()
->with('posts/photos', 'nowfoo.jpg');
对此:
$file->shouldReceive('move')
->once()
->with('posts/photos', '/nowfoo.jpg');