【问题标题】:Image uploading testing using phpunit and mockery使用 phpunit 和 mockery 进行图片上传测试
【发布时间】:2016-04-25 04:13:25
【问题描述】:

我正在尝试使用 Laravel 和 PhpUnit 测试图片上传。在测试课之外,它工作正常。但是当我运行测试时,我收到以下错误:

1) 应用\UploadTest::it_uploads_an_image_on_post Mockery\Exception\NoMatchingExpectationException:没有匹配的处理程序 为 Mockery_0_Symfony_Component_HttpFoundation_File_UploadedFile::move("posts/photos", “/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";

    }


}

【问题讨论】:

    标签: laravel phpunit laravel-5.1 mockery


    【解决方案1】:

    我认为问题只是一个错字。你应该改变期望:

    $file->shouldReceive('move')
            ->once()
            ->with('posts/photos', 'nowfoo.jpg');
    

    到这里:

    $file->shouldReceive('move')
            ->once()
            ->with('posts/photos', '/nowfoo.jpg');
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2015-06-15
      • 2017-08-16
      • 2014-03-21
      • 2014-06-15
      • 2013-08-26
      • 2019-06-18
      • 2011-10-10
      • 2015-03-07
      相关资源
      最近更新 更多