账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    laravel 开发拓展包改如何进行单元测试?
    75
    0
    1. 开发一款包测试功能需要手动测试,因此我想到了单元测试,然后参考passport的单元测试,但是我不知道该怎么写?mockery+phpunit在laravel包中该如何进行调用呢?我尝试去看mockery的文档也没有看懂?能帮我解决下么?谢谢!
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 濄期的埘堠ン 普通会员 1楼

      在 Laravel 开发中,我们通常使用 PHP 和 Laravel 的内置测试框架(如 phpunit)来进行单元测试。如果你想在扩展包中进行单元测试,你需要使用测试框架和扩展包的内部 API 来编写测试代码。

      以下是一个基本的步骤:

      1. 创建测试类:首先,你需要创建一个测试类,这个类将作为你的测试框架的入口点。你可以使用 phpunit 创建一个新的测试类,然后在类中定义你需要测试的代码。

      ```php namespace Tests;

      use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithLaravel; use Tests\TestCase; use Tests\Support\Facadesfait;

      class YourTest extends TestCase { use RefreshDatabase; use WithFaker; use WithLaravel;

      protected function setUp(): void
      {
          parent::setUp();
      
          $this->createDatabase();
      }
      
      public function testYourTest(): void
      {
          $this->assertDatabaseHas('your_table', ['your_column' => 'your_value']);
      }
      

      } ```

      1. 使用扩展包的 API:在你的测试类中,你可以使用扩展包的 API 来编写测试代码。例如,如果你正在使用 Laravel 的 prophecy 扩展,你可以使用它的 prophecy-listener 模块来监听预测事件。

      ```php namespace Tests;

      use Prophecy\Prophecy; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithLaravel; use Tests\Support\Facadesfait;

      class YourTest extends TestCase { use RefreshDatabase; use WithFaker; use WithLaravel;

      protected function setUp(): void
      {
          parent::setUp();
      
          $this->createDatabase();
      }
      
      public function testYourTest(): void
      {
          $ prophecy = $this->prophesize(YourNamespace::class);
          $prediction = $prophet->predict('your_event');
      
          $this->assertDatabaseHas('your_table', ['your_column' => 'your_value']);
          $this->assertDatabaseHas('your_table', ['your_column' => 'your预测结果']);
      }
      

      } ```

      请注意,这只是一个基本的例子,实际的测试可能会更复杂,需要考虑更多的因素,例如测试的覆盖率、依赖关系的测试等。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部