Friday, March 17, 2017

Laravel: Unit test Route::group with prefix

I'm new to Laravel and am following along with the Play by Play: Getting Started with Laravel 5 with Steven Maguire video in Pluralsight. This is a clean install of the lavarvel/laravel box in Vagrant. Laravel is at version 5.4.15 and PHPUnit is at version 5.7.16.

However, I can't seem to get my route tests to not fail as I add groups and controllers.

Here's my routes/web.php file:

<?php
Route::get('products', ['as' => 'products', function(){
    return App\Product::all();
}]);

Here's my tests/Unit/ExampleTest.php:

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
    public function testProductsList(){
        $this->get(route('products'))
            ->assertResponseOk();
    }
}

With just this basic setup, when I run my tests, I get this response: Error: Call to undefined method Illuminate\Http\Response::assertResponseOk()

After reading around, I restructured the test to this:

public function testProductsList(){
    $response = $this->get(route('products'));
    $this->assertEquals(200, $response->status());
}

This new structure succeeds, so I proceeded with the video and next we added a route group. Here's the route group in the routes\web.php file:

Route::group(['prefix' => 'api'], function(){
    Route::get('products', ['as' => 'products', function(){
        return App\Product::all();
    }]);
});

This also succeeds. The problems begin when I introduce Route::resource into the flow. The tests begin to fail. Here's the new routes\web.php file:

Route::group(['prefix' => 'api'], function(){
    Route::resource('products', 'ProductController', ['only' => ['index', 'store', 'update']]);
});

Here's the ProductController class in this file app/Http/Controllers/ProductController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        return 'response';
    }
    public function store(Request $request)
    {

    }
    public function update(Request $request, $id)
    {

    }
}

I used php artisan make:controller ProductController to create this controller. Here's the error that I now receive: InvalidArgumentException: Route [products] not defined.

I tried to change the adjusted testProductsList() method back to the original code and it still doesn't work.

Any insights into how I can get past these errors? I'd prefer to keep the original testProductsList() method code, however, if that's just not how it works anymore, then as long as I can get it working, I'm fine. Thanks in advance for your help and insight!



via Michael

Advertisement