【问题标题】:Can't perform a Laravel 4 action/route test more than once不能多次执行 Laravel 4 动作/路线测试
【发布时间】:2013-08-01 21:20:03
【问题描述】:

我遇到了 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 项目不会出现此问题。什么可能导致这种行为?您将如何追踪问题?

【问题讨论】:

    标签: testing laravel phpunit action laravel-routing


    【解决方案1】:

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

    更改为 include() 解决了问题中显示的问题

    【讨论】:

      猜你喜欢
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多