Dingo/API when Unit Testing: The version given was unknown or has no registered routes
<p>I built an API using <code>dingo/api 0.10.0</code>, <code>Laravel 5.1</code> and <code>lucadegasperi/oauth2-server-laravel": "^5.1"</code>.</p>

<p>All my routes work fine in Postman/Paw!</p>

<p>The problem appears when I try to test the API using <code>PHPUnit</code>.</p>

<p>This is part of my <code>route-api.php</code> file</p>

<pre><code>&lt;?php

$api = app('Dingo\Api\Routing\Router');

$api-&gt;version(['v1'], function ($api) {

$api-&gt;post('oauth/access_token', function () {
    return response(
        \LucaDegasperi\OAuth2Server\Facades\Authorizer::issueAccessToken()
    )-&gt;header('Content-Type', 'application/json');
});

$api-&gt;group(['middleware' =&gt; ['oauth', 'api.auth']], function ($api) {
    $api-&gt;post('/register', 'YPS\Http\Controllers\Api\UserController@register');
});
</code></pre>

<p>And this is my test file UserRegistrationTest.php</p>

<pre><code>class UserRegistrationTest extends ApiTestCase
{

public function setUp()
{
    parent::setUp();
    parent::afterApplicationCreated();
}

public function testRegisterSuccess()
{
    $data = factory(YPS\User::class)-&gt;make()-&gt;toArray();
    $data['password'] = 'password123';

    $this-&gt;post('api/register', $data, $this-&gt;headers)
        -&gt;seeStatusCode(201)
        -&gt;seeJson([
            'email' =&gt; $data['email'],
            'first_name' =&gt; $data['first_name'],
            'last_name' =&gt; $data['last_name'],
        ]);
}

public function testRegisterMissingParams()
{
    $this-&gt;post('api/register', [], $this-&gt;headers, $this-&gt;headers, $this-&gt;headers)-&gt;seeStatusCode(422);
}
}
</code></pre>

<p>The ApiTestCase simply retrieves a token and sets the headers.</p>

<pre><code>private function setHeaders()
{
    $this-&gt;headers = [
        'Accept' =&gt; 'application/vnd.yps.v1+json',
        'Authorization' =&gt; 'Bearer ' . $this-&gt;OAuthAccessToken,
    ];
}
</code></pre>

<p>Now, the weird part is that the first test <code>testRegisterSuccess</code> runs perfectly and returns the response I expect. But the second one <code>testRegisterMissingParams</code>, even though it's the same route, returns this, </p>

<pre><code>array:2 [
      "message" =&gt; "The version given was unknown or has no registered routes."
      "status_code" =&gt; 400
 ] 
</code></pre>

<p>I tracked the error and it is in the <code>Laravel adapter</code> here:</p>

<pre><code>public function dispatch(Request $request, $version)
{
    // it seems that the second time around can't find any routes with the key 'v1'
    if (! isset($this-&gt;routes[$version])) {
        throw new UnknownVersionException;
    }

    $routes = $this-&gt;mergeExistingRoutes($this-&gt;routes[$version]);

    $this-&gt;router-&gt;setRoutes($routes);

    return $this-&gt;router-&gt;dispatch($request);
}
</code></pre>

<p>And further more, if i run one test at a time (eg comment one out, run test and then comment the other and run test) i see the result expected in both tests. The problem is when i run multiple tests.</p>

<p>Any thoughts on that?</p>

<p>Thank you!</p>