使用assertsee时,Laravel在null上调用成员函数

时间:2017-06-14 01:38:21

标签: laravel phpunit

所以当我测试视图是否有这个字符串时,我的所有其他测试都在工作,除了这个。问题是我收到一条错误消息,指出被调用的方法是null,但是当我访问页面时它正在工作。

错误讯息:

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function getCode() on null
/home/vagrant/Code/team-stores/app/Http/Controllers/StoreManager/OrderTrackingController.php:20
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:55
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:44
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Route.php:203
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Route.php:160
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Router.php:574
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Router.php:576
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Router.php:535
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Router.php:513
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:174
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:102
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:149
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
/home/vagrant/Code/team-stores/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:234
/home/vagrant/Code/team-stores/tests/Feature/OrderTrackingTest.php:152

测试

public function an_admin_can_view_all_orders_with_their_status()
{
    $this->withoutMiddleWare();

    StoreSession::setMyStore($this->store->code);

    $orders = factory(CompletedOrder::class, 30)->create(['store_id' => $this->store->id]);

    foreach ($orders as $order) {
        factory(CompletedOrderItem::class, 5)->create(['completed_order_id' => $order->id]);
    }

    $response = $this->call("GET", route('list_completed_orders'));

    foreach ($orders as $order) {
        $response->assertSee($order->invoice()->getCode());
    }
}

控制器:

public function index()
{
    $store = StoreSession::getMyStore();

    $completedOrders = CompletedOrder::where('store_id', $store->getId() )->get();

    foreach ($completedOrders as $order) {
        Log::info($order->invoice()->getCode());
    }

    return view('store-manager.order-tracking.index', compact('store', 'completedOrders'));
}

提前致谢。

0 个答案:

没有答案