【问题标题】:Making Symfony2 unit tests more DRY by extending WebTestCase通过扩展 WebTestCase 使 Symfony2 单元测试更加 DRY
【发布时间】:2012-06-07 19:07:37
【问题描述】:

我的很多测试都有很多相同的 setUp()/tearDown() 内容。将相同的代码复制并粘贴到我的每一个单元测试中似乎很愚蠢。我想我想创建一个新的测试类来扩展 WebTestCase 我的其他测试可以扩展。

我的问题是我真的不知道怎么做。首先,在哪里放置这个新类最合适?我尝试在我的Tests 文件夹中制作一个,但是我的所有测试都无法真正找到该类。也许我只是不懂命名空间。

之前有没有人以我所说的方式扩展WebTestCase?如果有,你是怎么做到的?

【问题讨论】:

    标签: unit-testing symfony


    【解决方案1】:

    我没有这样做,但我可能会这样做

    src/Your/Bundle/Test/WebTestCase.php

    <?php
    
    namespace Your\Bundle\Test
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as WTC;
    
    class WebTestCase extends WTC
    {
      // Your implementation of WebTestCase
    }
    

    【讨论】:

    • 使用你的方法我得到一个错误:在测试中找不到类'Your\Bundle\Test\WebTestCase'。我怎么能强制 symfony 加载我的类?
    【解决方案2】:

    在我的测试中,我通常按照 Peter 建议的方式扩展 WebTestCase。此外,我使用 require_once 使 AppKernel 在我的 WebTestCase 中可用:

    <?php
    
    namespace My\Bundle\Tests;
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
    
    require_once(__DIR__ . "/../../../../app/AppKernel.php");
    
    class WebTestCase extends BaseWebTestCase
    {
      protected $_application;
    
      protected $_container;
    
      public function setUp()
      {
        $kernel = new \AppKernel("test", true);
        $kernel->boot();
        $this->_application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
        $this->_application->setAutoExit(false);
    
    ...
    

    然后我的测试如下所示:

    <?php
    
    namespace My\Bundle\Tests\Controller;
    
    use My\Bundle\Tests\WebTestCase;
    
    class DefaultControllerTest extends WebTestCase
    {
        public function testIndex()
        {
    ...
    

    【讨论】:

      【解决方案3】:

      您无需扩展 WebTestCase 以包含 AppKernel。您可以使用以下方法

          $client = static::createClient();
          self::$application = new Application($client->getKernel());
      

      【讨论】:

        【解决方案4】:

        这个问题确实很老,但它在 Google 上的排名很高,所以我想我会添加我的解决方案。

        我使用默认的setUp 方法和logIn 方法扩展了WebTestCase,以便我可以更轻松地运行经过身份验证的测试。

        您似乎无法将标准类添加到tests 目录,因为单元测试找不到它们。我不确定为什么。我的解决方案是添加一个目录src/_TestHelpers 并将我的助手类放在那里。

        所以我有:

        # src/_TestHelpers/ExtendedWTC.php
        namespace _TestHelpers;
        
        use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
        
        class ExtendedWTC extends WebTestCase
        {
            # Do your cool extending here
        }
        

        # tests/AppBundle/Controller/DefaultControllerTest.php
        namespace Tests\AppBundle;
        
        use _TestHelpers\ExtendedWTC
        
        class DefaultControllerTest extends ExtendedWTC
        {
            # Your tests here, using your cool extensions.
        }
        

        注意:我使用的是Symfony 3 目录结构,因此我的测试在tests/ 而不是src/AppBundle/Tests/

        我希望这对某人有所帮助...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-27
          • 2011-08-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多