나는 많은 예제를 읽었으며 내가 잘못하고있는 것을 볼 수 없다. 누군가 도와 줄 수 있다면 제발.Repository를 사용하는 PHPUnit Laravel 테스팅 컨트롤러
테스트를 실행할 때 오류가 발생합니다 (게시물 하단의 오류). 브라우저에서 페이지를 볼 때 발생하는 오류는 아닙니다. 저장소가 제대로 인스턴스화되지 않아 관련 메소드가 실행되지 않았기 때문에 이것이라고 생각하십니까? 또는 모의에서 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
응답의
내용 ($ 응답 -> 컨텐츠()) : home.blade.php에서
<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) :
@foreach ($mostRecent as $key => $publication)
그것은 분명한 것 같다 방법이 -> getRecent() 차례로 게시 저장소에서 getAll()을 호출하면 배열을 반환하지 않지만 그 이유는 알 수 없습니다.
블레이드가 가장 최근에 존재하지 않는 변수에 대해 불평하지 않고 있으며, foreach에서 유효하지 않다고 불평하고 있습니다.
Guzzle과 관련이 있으며 조롱 된 테스트 객체에서 내 API를 호출한다는 사실이 있습니까?
시간이 손실되었습니다.
감사합니다.
나는 그것에 묶여있는 계약보다는 직접 저장소를 조롱했다. $ this-> publicationRepository -> shouldReceive ('getRecent') -> once(); $ this-> app-> instance ('ShopApp \ Repositories \ localPublicationRepository', $ this-> publicationRepository); 그러나 이것은 같은 오류가 있습니다 :/ – Joel