【问题标题】:How may I test in CakePHP with Pest我如何在 CakePHP 中使用 Pest 进行测试
【发布时间】:2021-08-14 18:08:33
【问题描述】:

我的问题: 我如何在 CakePHP 中使用 Pest 测试一些 MVC 部分,例如: 当我使用 PHPUnit 时,我会输入:

公共类 ItemTest 扩展了 TestCase

它会显示我想要测试的东西,但是如何使用 Pest 来测试?

(Obs.:我对测试真的很陌生,所以我接受任何能让我进步的东西:))

【问题讨论】:

    标签: php testing model-view-controller cakephp php-pest


    【解决方案1】:

    在您使用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/phpunit 运行您的测试套件,而是使用$ ./vendor/bin/pest 运行它。

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 2012-04-22
      • 2017-04-09
      • 2013-09-15
      • 1970-01-01
      • 2013-11-18
      相关资源
      最近更新 更多