Question

I'm running into a Laravel 4 testing issue: An action/route test can only be run once, and it has to be the first test run. Any subsequent action/route test will fail with an exception before the assert is called.

  • route/action tests run as long as they are the first test run.
  • Non-route/action tests run normally, although they cause subsequent route/action tests to throw an exception

It's important to note that the tests in question don't fail, they throw an exception when the action is fired, for example:

Symfony\Component\Routing\Exception\RouteNotFoundException: Unable to generate a URL for the named route "home" as such route does not exist.

Sample test class:

class ExampleTest extends TestCase {

// passes
public function testOne()
{
    $class = MyApp::ApiResponse();
    $this->assertInstanceOf('\MyApp\Services\ApiResponse', $class);
}

// this fails unless moved the top of the file
public function testRoute()
{
    $this->route('GET','home');
    $this->assertTrue($this->client->getResponse()->isOk());
}

// passes
public function testTwo()
{
    $class = MyApp::ProjectService();
    $this->assertInstanceOf('\MyApp\Services\ProjectService', $class);
}

}

This is implementation-specific, a fresh Laravel 4 project does not exhibit the issue. What could be causing this behaviour? How would you go about tracking down the problem?

Était-ce utile?

La solution

In this case, the routes file was being called using an include_once call. When subsequent tests were run the routes were empty.

Changing to include() fixed the issue exhibited in the question

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top