0

php artisan make:test -- unit UnitTestClassName使用扩展创建的单元测试\Tests\TestCase

功能测试创建php artisan make:test FunctionalTestClassName也扩展\Tests\TestCase

\Tests\TestCase延伸Illuminate\Foundation\Testing\TestCase

但是,Laravel Dusk 浏览器测试用例使用php artisan dusk:make DuskTestClassNameextend创建\Tests\DuskTestCase,而后者又扩展Laravel\Dusk\TestCase

似乎没有通用类,我可以向其中添加要测试的用户之类的内容(至少在供应商文件夹之外)。

为了允许共享代码,我在 app\Traits\TestData.php 中创建了一个特征:

<?php
namespace App\Traits;
trait TestData {
  static $testUserId = 40;
  public static function checkTestData() {
    // returns an array of strings describing any problems with the test data setup.
    ...
  }
  ...
}

use在我的测试中具有这种特征。有没有更好的办法?具体来说,我缺少一个常见的测试类吗?

我遇到的具体问题是我的特征没有扩展任何TestCase,所以我不能调用像

$this->assertNotNull($testUser,"Test user not found");

在我的特质中,所以我需要在 TestCase 和 DuskTestCase 中复制该代码。

4

1 回答 1

0

只需在 trait 中静态调用 assert 方法:

self::assertNotNull($testUser,"Test user not found");
于 2019-11-08T01:45:04.567 回答