不能多次执行Laravel 4动作/路线测试

时间:2013-08-01 05:23:57

标签: testing laravel phpunit action laravel-routing

我遇到了Laravel 4测试问题:操作/路由测试只能运行一次,而且必须是第一次测试运行。在调用断言之前,任何后续的动作/路由测试都将失败并发生异常。

  • 路由/动作测试只要是第一次测试运行就会运行。
  • 非路由/操作测试正常运行,但它们会导致后续路由/操作测试抛出异常

重要的是要注意所讨论的测试不会失败,它们会在触发操作时抛出异常,例如:

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

示例测试类:

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

}

这是特定于实现的,新的Laravel 4项目没有出现问题。什么可能导致这种行为?你会如何追查问题呢?

1 个答案:

答案 0 :(得分:1)

在这种情况下,使用include_once调用调用routes文件。当后续测试运行时,路线是空的。

更改为include()修复了问题中显示的问题