2014-02-26 42 views
2

我正在开发一个使用Laravel 4并尝试遵循TDD的应用程序。我按照Jeffrey Way或Philip Brown的教程,使用我的数据库的存储库。我以前遇到过这个问题(Mockery not calling method from repository (interface)),但现在在我的测试中一切正常。不过,我得到一个错误试图嘲弄2个库在同一个测试,像这样:与Laravel 4同时嘲笑2个数据存储库4

class PedidosControllerTest extends TestCase { 

    private $mock; 
    private $pedidoModelMock; 
    private $mockCliente; 
    private $clienteModelMock; 

    function setUp() { 
     parent::setUp(); 
     $this->mock = $this->mock('repositories\canarias\PedidoDbRepository'); 
     $this->pedidoModelMock = Mockery::mock('Pedido'); 
     $this->mockCliente = $this->mock('repositories\canarias\ClienteDbRepository'); 
     $this->clienteModelMock = Mockery::mock('Cliente'); 
    } 


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

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

     return $mock; 
    } 

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


    public function testIndexWithClient() 
    { 
     $nestedView = 'pedidos.index'; 
     $this->registerNestedView($nestedView); 

     $this->mockCliente 
      ->shouldReceive('find') 
      ->once() 
      ->with(698) 
      ->andReturn($this->clienteModelMock); 

     $this->mock 
      ->shouldReceive('findAllFromCliente') 
      ->once() 
      ->with(698) 
      ->andReturn($this->pedidoModelMock); 

     $this->clienteModelMock 
      ->shouldReceive('getAttribute') 
      ->once() 
      ->with('nombre') 
      ->andReturn('Pepito'); 

     $this->call('GET', '/clientes/698/pedidos'); 
     $this->assertResponseOk(); 
     $this->assertViewHas('pageAttributes'); 
     $this->assertViewHas('contenido'); 
     $this->assertNestedViewHas($nestedView, 'pedidos'); 
     $this->assertNestedViewHas($nestedView, 'cliente'); 
    } 
} 

从我测试(没有双关语意),这个问题似乎与此代码由共享有关这两个$ this-> mock和$ this-> mockCliente:

Mockery :: mock('Model',$ class);

我收到一个错误,指出Model类不存在。在测试的其他功能中,我只使用ONE mock,确实找到了这个类,所以它与拼写错误的名称或类似的名称无关。

是模特类第一次莫名其妙地“迷失”了吗?

+1

你能找出问题?创建一个只包含这两行的测试:Mockery :: mock('Pedido');和Mockery :: mock('Cliente');并查看是否仍未找到型号 – carlosdubusm

+0

确保您使用正确的名称空间。并尝试使用App :: make(repositories \ canarias \ PedidoDbRepository) –

回答

0

事实证明,这确实是一个问题,类被拼错(白痴错误,我知道)。它应该是

$this->mockCliente = $this->mock('repositories\canarias\ClientesDbRepository'); 

,而不是

$this->mockCliente = $this->mock('repositories\canarias\ClienteDbRepository');