我如何在CakePHP中测试Pest

eimct9ow  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(164)

我的问题:我如何在CakePHP中用Pest测试一些MVC部分,例如:当我使用PHPUnit时,我会输入:
公共类ItemTest扩展TestCase
它会显示我想测试的东西,但是Pest怎么做呢?
(Obs.:我真的是新的测试,所以我接受任何会让我提高:))

hof1towb

hof1towb1#

在你完成installed Pest之后,你只需要按照它的语法和文档,删除所有的OOP代码,只使用test() or it()。文件名将是相同的。这是一个来自CakePHP文档的例子:

class ArticlesTableTest extends TestCase
{
    public $fixtures = ['app.Articles'];

    public function setUp()
    {
        parent::setUp();
        $this->Articles = TableRegistry::getTableLocator()->get('Articles');
    }

    public function testFindPublished()
    {
        $query = $this->Articles->find('published');
        $this->assertInstanceOf('Cake\ORM\Query', $query);
        $result = $query->enableHydration(false)->toArray();
        $expected = [
            ['id' => 1, 'title' => 'First Article'],
            ['id' => 2, 'title' => 'Second Article'],
            ['id' => 3, 'title' => 'Third Article']
        ];

        $this->assertEquals($expected, $result);
    }
}

有了Pest,它将变成:

beforeEach(function () {
    $this->Articles = TableRegistry::getTableLocator()->get('Articles');
});

test('findPublished', function () {
    $query = $this->Articles->find('published');

    expect($query)->toBeInstanceOf('Cake\ORM\Query');

    $result = $query->enableHydration(false)->toArray();
    $expected = [
        ['id' => 1, 'title' => 'First Article'],
        ['id' => 2, 'title' => 'Second Article'],
        ['id' => 3, 'title' => 'Third Article']
    ];

    expect($result)->toBe($expected);
});

请注意,toBeInstanceOf()toBe()是可用Pest expectations的一部分。
最后,您将使用$ ./vendor/bin/pest来运行测试套件,而不是使用$ ./vendor/bin/phpunit来运行。

相关问题