【问题标题】:Yii2 + Codeception: How to use fixtures?Yii2 + Codeception:如何使用fixture?
【发布时间】:2017-04-11 15:39:13
【问题描述】:

我使用 Codeception 为我的 Yii2 应用程序编写了一个简单的测试。我不想使用真正的 MySQL 数据库,而是使用固定装置。

代码如下:

测试/PersonTest.php

namespace app\tests\unit\models;

use tests\fixtures;
use app\controllers;

class PersonTest extends \Codeception\Test\Unit
{
    protected $tester;
    public $appConfig = '@app/config/main.php';

    protected function _before(){ }
    protected function _after(){ }

    public function _fixtures()
    {
        return [ 'Person' => fixtures\PersonFixture::className() ];
    }

    public function testUser(){
        $person = Person::findOne( [ "id" => 1 ] );
        $userId = isset( $person->id ) ? $person->id : false;
        $this->assertEquals( 1, $userId );
    }
}

tests/fixtures/data/Person.php

return [
    'person1' => [
        'id'            => 1,
        'firstname'     => 'Foo',
        'lastname'      => 'Bar',

    ],
];

测试/夹具/Person.php

namespace tests\fixtures;

use yii\test\ActiveFixture;

class PersonFixture extends ActiveFixture
{
    public $modelClass = 'app\models\Person';
}

当我运行测试时,我得到了错误:

[错误] 找不到类“tests\fixtures\PersonFixture”

我尝试了 100 种不同的方法,但我无法成功。如果这个简单的例子对我有用,我可以创建真正的测试。

【问题讨论】:

    标签: unit-testing yii yii2 codeception fixtures


    【解决方案1】:

    使用 Codeception 2.3.8,您可以这样做:

    定义你的夹具(并像你的问题一样拥有数据文件)

    namespace app\tests\fixtures;
    
    class PersonFixture extends \yii\test\ActiveFixture {
    
        public $modelClass = 'app\models\Person';
    
    }
    

    然后编写你的测试

    namespace app\tests\unit;
    
    class PersonTest extends \Codeception\Test\Unit {
    
        public function _fixtures() {
            return [
                'persons'   => 'app\tests\fixtures\PersonFixture',
            ];
        }
    
        public function testUser() {
            $person1 = $this->tester->grabFixture('persons', 'person1');
            $this->assertEquals(1, $person1->id);
        }
    
    }
    

    就是这样。

    【讨论】:

      【解决方案2】:
      with codeception 4.0.3 you can run your fixture by following the steps...
      create fixtures folder inside test
      [fixture folder][1]
      
        [1]: https://i.stack.imgur.com/uK9Cy.png
      inside your fixtures/data/book.php
      
      <?php
      return [
          'book1' => [
              'title' => 'lmayert',
              'isbn' => 'Ibn-098',
          ],
          'user2' => [
              'title' => 'napoleon69',
              'isbn' => 'Ibn-042',        
          ],
      ];
      
      your fixtures/BookFixture be like this:
      
      <?php
      
      namespace app\tests\fixtures;
      
      use yii\test\ActiveFixture;
      
      /**
       * 
       */
      class BookFixture extends ActiveFixture
      {
          public $modelClass = 'app\models\Book';
      }
      
      Now the tests/unit/BookTest be like this
      
      <?php 
      use app\tests\unit\fixtures\BookFixture;
      class BookTest extends \Codeception\Test\Unit
      {
          /**
           * @var \UnitTester
           */
          protected $tester;
      
          protected function _before()
          {
          }
      
          protected function _after()
          {
          }
      
          public function _fixtures() {
              return [
                  'books' => 'app\tests\fixtures\BookFixture',
              ];
          }
          // tests
          public function testBook()
          {
              $book1 = $this->tester->grabFixture('books','book1');
              $this->assertEquals(1,$book1->id);
          }
      }
      
      I hope this will help
      

      【讨论】:

        【解决方案3】:

        您必须将夹具文件名从Person.php 更改为PersonFixture.php,它将开始工作。

        【讨论】:

          【解决方案4】:

          您必须使用 yii2-codeception 扩展程序,它会为您自动加载固定装置。

          安装后,yii\codeception\DbTestCase 类可用,PersonTest 应该扩展它。

          Person 装置应具有如下命名空间:app\tests\fixtures

          【讨论】:

          猜你喜欢
          • 2015-03-18
          • 1970-01-01
          • 2015-07-16
          • 2014-12-15
          • 2018-04-06
          • 2015-07-21
          • 1970-01-01
          • 1970-01-01
          • 2021-02-27
          相关资源
          最近更新 更多