2017-03-23 17 views
0

私は多くの例を読んできましたが、私が間違っていることを見ることができません。リポジトリを使用するPHPUnit Laravel Testing Controller

テストを実行しているときにエラーが表示されます(ポストの最後にエラーがあります)。ブラウザでページを表示してもエラーになりません。リポジトリが適切にインスタンス化されていないため、関連するメソッドが起動されないためですまたは、モック内のAPIコールに関する問題。

コントローラー:

namespace ShopApp\Http\Controllers\StoreFront; 

use Illuminate\Http\Request; 
use ShopApp\Http\Requests; 
use ShopApp\Http\Controllers\Controller; 
use ShopApp\Repositories\Contracts\CategoryRepositoryContract; 
use ShopApp\Repositories\Contracts\PublicationRepositoryContract; 

class PagesController extends Controller 
{ 

    private $publication; 
    private $category; 


    public function __construct(PublicationRepositoryContract $publication, CategoryRepositoryContract $category){ 

    $this->publication = $publication; 
    $this->category = $category; 

    } 

    /** 
    * Homepage. 
    * @return view 
    * @internal param PublicationRepositoryContract $publication 
    * @internal param CategoryRepositoryContract $category 
    */ 
    public function home() 
    { 
     $mostRecent = $this->publication->getRecent(); 

     return view('pages/home')->with(compact('mostRecent')); 

    } 


} 

公開レポジトリ:

<?php 

namespace ShopApp\Repositories; 

use ShopApp\Models\API\APIModel; 
use GuzzleHttp\Client as GuzzleClient; 
use Illuminate\Support\Facades\Config; 
use ShopApp\Repositories\Contracts\PublicationRepositoryContract; 

class localPublicationRepository extends APIModel implements PublicationRepositoryContract 
{ 


    private $end_point; // where are we talking to? 
    public $response; //what did we get back? 

    public function __construct(GuzzleClient $client){ 

     parent::__construct(new $client(['base_uri' => Config::get('customerprovider.local.api.base_uri'), 'http_errors' => true])); 

     $this->end_point = 'Publications'; 

    } 


    /** 
    * Get all publications 
    */ 
    public function getAll(){ 

     $this->response = $this->get($this->end_point); 

     $publications_with_slugs = $this->assembleSlugs($this->response); 

     return $publications_with_slugs; 

    } 


    /** 
    * Get recent publications 
    */ 
    public function getRecent(){ 

     return $this->getAll(); //@todo - update this to just get the most recent 

    } 


} 

試験:

<?php 

namespace Tests\Unit\Controllers; 

use Tests\TestCase; 
use Mockery as m; 

class PagesControllerTest extends TestCase 
{ 

    public $publicationRepositoryContract; 

    /** 
    * Setup mocks etc 
    */ 
    public function setUp() 
    { 

     parent::setup(); 

     $this->publicationRepositoryContract = m::mock('ShopApp\Repositories\Contracts\PublicationRepositoryContract'); 

    } 


    /** 
    * Teardown mocks 
    */ 
    public function tearDown() 
    { 
     m::close(); 
     parent::tearDown(); 
    } 


    /** 
    * A basic test example. 
    * 
    * @return void 
    */ 
    public function testHomepage() 
    { 


     $this->publicationRepositoryContract 
      ->shouldReceive('getRecent') 
      ->once(); 

     $this->app->instance('ShopApp\Repositories\Contracts\PublicationRepositoryContract', $this->publicationRepositoryContract); 

     $response = $this->call('GET', '/'); 

     $response->assertStatus(200); 

     // getData() returns all vars attached to the response. 
     $mostRecent = $response->original->getData()['mostRecent']; 

     $response->assertViewHas('mostRecent'); 

     $this->assertInstanceOf('Array', $mostRecent); 


    } 


} 

テストエラー:

Expected status code 200 but received 500. 
Failed asserting that false is true. 
/home/vagrant/Code/imsnews-site/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:61 
/home/vagrant/Code/imsnews-site/tests/Unit/Controllers/PagesControllerTest.php:53 
レスポンス($対応 - >コンテンツ())のの

内容:

<span class="exception_title"><abbr title="ErrorException">ErrorException</abbr> in <a title="/home/vagrant/Code/imsnews-site/storage/framework/views/229655ca372490c9c0b1f5e7e2d4e91e6d3bbf6c.php line 262">229655ca372490c9c0b1f5e7e2d4e91e6d3bbf6c.php line 262</a>:</span>\n 
          <span class="exception_message">Invalid argument supplied for foreach() (View: /home/vagrant/Code/imsnews-site/resources/views/pages/home.blade.php)</span>\n 

ライン262 home.blade.phpから:

@foreach ($mostRecent as $key => $publication) 

それは方法ことが明らかに思える - > getRecent()、パブリケーションリポジトリの - > getAll()を呼び出すと配列が返されませんが、理由はわかりません。

ブレードは、現在存在しない変数に不満を募らず、foreachでは無効であると不平を言っています。

これはGuzzleとは関係がありますが、私のAPIは模擬テストオブジェクトから呼び出されていますか?

時間が..

感謝を失ってきた、助けてください。

+0

私は、バインドされているコントラクトではなく、リポジトリを直接模擬しようとしました。 $ this-> publicationRepository - > shouldReceive( 'getRecent') - > once(); $ this-> app-> instance( 'ShopApp \ Repositories \ localPublicationRepository'、$ this-> publicationRepository); これは同じエラーがあります:/ – Joel

答えて

0

TL; DR:

キーあなたが持っている必要がありました - > andReturn([]);そのような試験では、上:

$this->publicationRepositoryContract 
      ->shouldReceive('getRecent') 
      ->once()->andReturn([]); 

私のテストだけ持っていた:

$this->publicationRepositoryContract 
       ->shouldReceive('getRecent') 
       ->once(); 

感謝をAYOにこれを指摘するために。それは私のテストの他の部分を削除した後にのみ明らかになった。

0

コンクリートレポジトリを模擬して、コンテナ内の契約用に交換してみてください。あなたは契約を嘲笑しているようで、あなたのコンテナの同じ契約のためにそれを交換しているようです。

+0

このような意味ですか? '$ this-> publicationRepository = m :: mock( 'ShopApp \ Repositories \ localPublicationRepository'); の$ this - > categoryRepository = M ::モック( 'ShopApp \リポジトリ\ localCategoryRepository'); ' と'の$ this - > APP->インスタンス( 'ShopApp \リポジトリ\契約\ PublicationRepositoryContract'、ます$ this- > publicationRepository); 残念ながら私は同じエラーが発生します:/助けを提供してくれてありがとう、それは私を怒らせています。 – Joel

+0

コメントでコードを適切に書式設定することができないのはなぜですか? – Joel

+0

実装ではどのように契約をバインドしていますか? –

関連する問題