【问题标题】:Loading Fixtures for functional tests in Symfony 2: Call to undefined method MyControllerTest::loadFixtures())在 Symfony 2 中为功能测试加载夹具:调用未定义的方法 MyControllerTest::loadFixtures())
【发布时间】:2015-07-27 12:43:21
【问题描述】:

我正在尝试找到一种简单的方法来在 Symfony 2.6 中加载我的设备以运行功能测试。这是一个很常见的问题,已经被问过好几次了,但到目前为止我找到的答案并没有达到我的预期:

  • Some 依赖于从功能测试内部运行命令行。
  • Other 手动运行每个已定义的固定装置,然后负责创建和删除数据库。

对于我认为非常标准的任务,这两种情况(使用语句和手动代码)都有很多开销。

另一方面,这些相同的帖子推荐 LiipFunctionalTestBundle。去吧,这是我在安装说明中读到的:

编写夹具类并从捆绑包中调用 loadFixtures() 方法 测试\WebTestCase 类。请注意 loadFixtures() 将在加载夹具之前从数据库中删除内容。”

所以我尝试了...

namespace AppBundle\Test\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class MyControllerTest extends WebTestCase
{
    public function setUp()
    {
        $classes = array(
                'AppBundle\DataFixtures\LoadUserData',
        );
        $this->loadFixtures($classes);
    }
    ...
}

没有运气:

在第 15 行调用 /gitrepos/myproject/src/AppBundle/Tests/Controller/MyControllerTest.php 中未定义的方法 AppBundle\Tests\Controller\MyControllerTest::loadFixtures()

静态调用会出现同样的错误...

self:loadFixtures($classes);

我真的认为我遗漏了一些非常明显的东西。任何人都可以让我重回正轨吗?

【问题讨论】:

  • LiipFunctionalTestBundle 期望 setup() 包含一个数组 $classes = array(...) 的灯具,然后 $this->loadFixtures($classes); 您的设置显示没有类,loadFixtures() 没有任何类数组。
  • @geoB 错误消息表明这不是问题。不过,我更新了帖子,以避免混淆。谢谢指正。
  • @dragoste 我在这篇文章中没有看到任何关于 loadFixtures 函数的提及。此外,它已经有 2 年历史了,而且这些捆绑包似乎已经更新了很多。我确实相信今天有更好的方法来做到这一点。
  • @mika 抱歉,没有仔细阅读您的问题。我可以看到您使用Oro\Bundle\TestFrameworkBundle\Test\WebTestCase 作为基类,而我认为您应该使用Liip\FunctionalTestBundle\Test\WebTestCase 才能调用此方法。
  • @dragoste 哦,是的...我自己太快地浏览了 Liip 安装说明:我使用的是 Symfony\Bundle\FrameworkBundle\Test\WebTestCase;它没有 loadFixtures 功能。如果您将评论作为答案,那将是正确的。非常感谢!

标签: symfony doctrine-orm functional-testing fixtures


【解决方案1】:

我可以看到你正在使用

Oro\Bundle\TestFrameworkBundle\Test\WebTestCase

作为基类,我认为你应该使用

Liip\FunctionalTestBundle\Test\WebTestCase

能够调用此方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 2013-06-10
    • 1970-01-01
    • 2016-08-04
    • 2013-01-23
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多