【发布时间】:2026-01-22 16:50:01
【问题描述】:
我是一名新手开发人员,我正在尝试在我未开发的现有应用程序中设置 phpUnit 和基本测试套件。我正在浏览 laravel 文档并尝试运行一些基本测试。当我尝试运行以下内容时:
$response = $this->action('GET', 'AlertsController@bannerAlerts');
我得到以下异常:
AlertsControllerTest::testIndexRoute
ErrorException: Trying to get property of non-object
我调用的方法是:
public function count() {
$statusIds = DB::table('alert_status')->where('user_id', $this->crmUser->id)->where('is_new', 0)->lists('alert_id');
$count = DB::table('alerts')->WhereNotIn('id', $statusIds)->count();
return Response::json($count);
}
有人知道我为什么会收到这个错误吗?据我所知,我并不想获得任何东西的属性。我只是想调用路线。
抛出错误的测试方法是:
public function testIndexRoute() {
$this->crmUser = new stdClass();
$this->crmUser->id = new stdClass();
$this->crmUser->id = 1;
$response = $this->action('GET','AlertsController@bannerAlerts');
$count = $response->original; $this->assertResponseOk();
}
【问题讨论】: