【发布时间】:2014-08-11 06:58:34
【问题描述】:
我刚开始学习如何在 Laravel 中进行测试。虽然我遇到了一些问题.. 我正在测试我的控制器并想检查视图是否分配了变量。
我的控制器代码:
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 循环来显示所有帖子:
@foreach ($posts as $post)
{{post->id}}
@endforeach
最后但并非最不重要的是我的测试文件:
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');
}
}
现在问题来了,当我运行“phpunit”时出现以下错误:
ErrorException:为 foreach() 提供的参数无效
任何想法为什么 phpunit 返回此错误?
【问题讨论】:
标签: testing laravel controller tdd