Mockery没有从存储库(接口)调用方法

时间:2013-12-04 03:55:22

标签: php testing laravel repository-pattern mockery

我正在尝试使用此测试来测试我的控制器(我正在使用Laravel,如果这很重要):

<?php
use Zizaco\FactoryMuff\Facade\FactoryMuff;

class ProjectControllerTest extends TestCase
{
    public function setUp()
    {
        parent::setUp();

        $this->mock = $this->mock('Dumminvoicing\Storage\Project\ProjectRepositoryInterface');
    }

    public function mock($class)
    {
        $mock = Mockery::mock($class);

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

        return $mock;
    }

    protected function tearDown()
    {
        Mockery::close();
    }

    public function testRedirectWhenNotLogged()
    {
        Route::enableFilters();
        $response = $this->call('GET', 'projects');
        $this->assertRedirectedToAction('UserController@getLogin');
    }

    public function testAllowedWhenLogged()
    {
        Route::enableFilters();
        //Create user and log in
        $user = FactoryMuff::create('User');
        $this->be($user);
        $response = $this->call('GET', 'projects');
        $this->assertResponseOk();
    }

    public function testIndex()
    {
        $this->mock->shouldReceive('all')->once();
        $this->call('GET', 'projects');
        $this->assertViewHas('projects');
    }

}

遵循这些教程http://culttt.com/2013/07/08/creating-flexible-controllers-in-laravel-4-using-repositories/ http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/我使用存储库来避免将数据库与测试耦合。所以我有这两个额外的课程:

<?php
namespace Dumminvoicing\Storage\Project;

use Project;
class EloquentProjectRepository implements ProjectRepository
{
    public function all()
    {
        return Project::all();
    }

    public function find($id)
    {
        return Project::find($id);
    }
}

<?php
namespace Dumminvoicing\Storage\Project;

interface ProjectRepository
{
    public function all();
    public function find($id);
}

当我运行测试时,我收到此错误:

有1个错误:

1)ProjectControllerTest :: testIndex Mockery \ Exception \ InvalidCountException:应该调用来自Mockery_2143809533_Dumminvoicing_Storage_Project_ProjectRepositoryInterface的方法all()  恰好1次,但被称为0次。

控制器的索引方法在浏览器中正常工作:

     use Dumminvoicing\Storage\Project\ProjectRepository as Project;

    class ProjectsController extends \BaseController
    {
        protected $project;

        public function __construct(Project $project)
        {
            $this->project = $project;

            $this->beforeFilter('auth');
        }
    }
/**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $data['projects'] = $this->project->all();
        return View::make('projects.index', $data) ;
    }

那为什么它在测试中失败了?为什么“全部”没有被召唤?

1 个答案:

答案 0 :(得分:1)

如果必须对用户进行身份验证以使用index方法,则需要对每个测试进行身份验证。

由于正在重定向用户,因此未调用all

创建authentication方法,每次需要验证请求时都可以调用该方法。

要查看测试失败的原因,请在执行断言之前转储响应。

修改

问题是你嘲笑Dumminvoicing\Storage\Project\ProjectRepositoryInterface但它应该是Dumminvoicing\Storage\Project\ProjectRepository

如果更正了命名空间并将$this->mock->shouldReceive('all')->once();添加到testAllowedWhenLogged()方法,则测试将正确传递。