Mockery“shouldReceive() - > once()”throw“正好调用了1次”错误“

时间:2015-09-12 00:59:52

标签: php laravel phpunit ioc-container mockery

我是关于$ this-> close()在我的单元测试中放弃Mockery。这是正在发生的事情,我正在使用Laravel 5.1,我正在尝试使用PHPUnit中的Mockery测试我的存储库模式抽象。我已经按照教程,倾注了StackOverflow问题,所以它不是重复的。当你在这里看到有关模块的任何信息时,它就是PingPong Sky Modules软件包。

基本上,当我尝试模拟存储库接口并设置shouldReceive('create')->with([])->once()时,Mockery抛出:

Mockery\Exception\InvalidCountException: Method create(array()) from Mockery_0_Modules_Documents_Repositories_DocumentRepositoryInterface should be called exactly 1 times but called 0 times.

DocumentsTest.php

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;

class FileUploadTest extends TestCase {

    use WithoutMiddleware;

    public function mock($class){

        $mock = Mockery::mock($class);

        $this->app->instance($class, $mock);

        return $mock;
    }

    public function testFileUpload(){

        // Mock the Repository
        $mock = $this->mock('\Modules\Documents\Repositories\DocumentRepositoryInterface');

        $mock->shouldReceive('create')->with([])->once();

        $this->call('POST', '/documents', [], [], []);
        $this->assertResponseStatus(201);
    }
}

DocumentRepositoryInterface.php

<?php namespace Modules\Documents\Repositories;

interface DocumentRepositoryInterface {

    public function create(array $data);

}

DatabaseDocumentRepository.php

<?php namespace Modules\Documents\Repositories;

use Modules\Documents\Repositories\DocumentRepositoryInterface;
use \Illuminate\Database\Eloquent\Model;

class DatabaseDocumentRepository implements DocumentRepositoryInterface {

    protected $documents;

    public function __construct(Model $documents) {
        $this->documents = $documents;
    }

    public function create(array $data) {
        // Eloquent code.
        return "response";
    }

}

Document.php

<?php namespace Modules\Documents\Entities;

use Illuminate\Database\Eloquent\Model;

class Document extends Model {

    protected $fillable = [];

}

routes.php文件

$this->app->bind(
        'Modules\Documents\Repositories\DocumentRepositoryInterface', function(){ 
    return new Modules\Documents\Repositories\DatabaseDocumentRepository(new Modules\Documents\Entities\Document());
});

Route::group(['prefix' => 'documents', 'namespace' => 'Modules\Documents\Http\Controllers'], function(){
    Route::post('/', ['as' => '/', 'uses'=> 'DocumentsController@create']);
});

DocumentsController.php

<?php namespace Modules\Documents\Http\Controllers;

use Modules\Documents\Repositories\DocumentRepositoryInterface;
use Pingpong\Modules\Routing\Controller;
use Module;
use Illuminate\Http\Response;
use Illuminate\Http\Request;

class DocumentsController extends Controller {
    private $documents;

    public function __construct(DocumentRepositoryInterface $doc){

        $this->documents = $doc;
    }

    public function create(Request $request){
        $this->documents->create([]);

        return response("", Response::HTTP_CREATED);
    }
}

我想说它与未被注入到DocumentsController中的模拟对象有关,因为调用了create()函数。我在create函数中放了一个print_r,它显示在我的控制台中。这很奇怪,它也可能与PingPong Sky Modules有关。我做错了什么或不做什么?

0 个答案:

没有答案