首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2 +协同欺骗:如何使用夹具?

Yii2 +协同欺骗:如何使用夹具?
EN

Stack Overflow用户
提问于 2017-04-11 15:39:13
回答 3查看 6.3K关注 0票数 6

我使用协同欺骗为我的Yii2应用程序编写了一个简单的测试。我不使用真正的MySQL db,而是使用固定设备。

以下是代码:

tests/PersonTest.php

代码语言:javascript
复制
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 );
    }
}

测试/固定/数据/Person.php

代码语言:javascript
复制
return [
    'person1' => [
        'id'            => 1,
        'firstname'     => 'Foo',
        'lastname'      => 'Bar',

    ],
];

测试/固定/Person.php

代码语言:javascript
复制
namespace tests\fixtures;

use yii\test\ActiveFixture;

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

当我运行测试时,我只得到一个错误:

错误类‘tests\ not \PersonFixture’未找到

我尝试了100种不同的东西,但我无法使它工作。如果这个简单的例子对我有用,我可以创建真正的测试。

EN

回答 3

Stack Overflow用户

发布于 2018-02-23 13:28:02

使用协同欺骗2.3.8您可以这样做:

定义您的夹具(并有数据文件,就像您在您的问题)

代码语言:javascript
复制
namespace app\tests\fixtures;

class PersonFixture extends \yii\test\ActiveFixture {

    public $modelClass = 'app\models\Person';

}

写你的测试

代码语言:javascript
复制
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);
    }

}

就这样。

票数 3
EN

Stack Overflow用户

发布于 2020-05-02 12:14:30

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2017-04-12 05:07:46

您必须使用yii2-codeception扩展,这将为您自动安装夹具。

安装它之后,您可以使用类yii\codeception\DbTestCasePersonTest应该对它进行扩展。

Person夹具应该具有如下名称空间:app\tests\fixtures

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43350500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档