Laravel에서 테스트하는 법을 배우기 시작했습니다. 그래도 몇 가지 문제가 발생했습니다. 내 컨트롤러를 테스트 중이며보기에 변수가 할당되었는지 확인하고 싶습니다. Laravel Testing Error
내 컨트롤러 코드 :class PagesController extends \BaseController {
protected $post;
public function __construct(Post $post) {
$this->post = $post;
}
public function index() {
$posts = $this->post->all();
return View::make('hello', ['posts' => $posts]);
}
}
그리고 내 모든 게시글보기 표시하는 foreach 루프를 포함 : 이제
class PostControllerTest extends TestCase {
public function __construct()
{
// We have no interest in testing Eloquent
$this->mock = Mockery::mock('Eloquent', 'Post');
}
public function tearDown()
{
Mockery::close();
}
public function testIndex() {
$this->mock->shouldReceive('all')->once()->andReturn('foo');
$this->app->instance('Post', $this->mock);
$this->call('GET', '/');
$this->assertViewHas('posts');
}
}
:
@foreach ($posts as $post)
{{post->id}}
@endforeach
마지막으로 내 테스트 파일을 문제가 발생하면 "phpunit"을 실행하면 다음 오류 메시지가 나타납니다 :
오류 : foreach()에 잘못된 인수가 제공되었습니다.
phpunit가이 오류를 반환하는 이유는 무엇입니까?
당신은뿐만 아니라보기를 조롱한다
감사합니다. 이것은 작동하지만, {{posts}}를 추가 할 때만 Array : 문자열 변환으로 오류가 발생합니다. 이것도 해결할 수있는 방법이 있습니까? –
FactoryMuff를 사용하여 다음과 같은 빠른 게시물을 작성했습니다. $ mockPost = FactoryMuff :: create ('Post'); 그러나 {{$ posts}}를 (를) 호출 할 때 문제가 해결되지는 않지만 게시물의 다른 모든 필드 (예 : 본문)를 채 웁니다. –
배열이기 때문에 {{posts}}를 에코 할 수 없습니다. 루프를 반복하고 질문의 코드와 같이 개별 항목을 표시해야합니다. –