0

So all my other tests are working except this one when I test the view if it has this string or not. Problem is I'm getting an error message that the method being called is on null but is working when I access the page.

Error message:

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

Test:

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());
    }
}

Controller:

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'));
}

Thanks in advance.

  • What your `$completedOrders` get ? Try `dd($completedOrders)` and post results. – Nauman Zafar Jun 14 '17 at 05:20
  • @NaumanZafar, thank you for your time but it seems that the problem that causing the error is the factory were not properly created with the proper model ID. Silly me. – aomanansala Jun 14 '17 at 07:18
  • glad you spotted the issue yourself ! – Nauman Zafar Jun 14 '17 at 07:34
  • 1
    Looks like you have not added any invoices to the orders ... - For detailed help on the error message, see: ["Reference - What does this error mean in PHP?"](https://stackoverflow.com/q/12769982/367456), in the end this is independent to laravel or phpunit and just related to bare PHP. – hakre Jun 19 '17 at 11:52

0 Answers0