Laravel - Artisan 命令



Laravel 5.7 附帶處理和測試新命令的新方法。它包含一項測試 artisan 命令的新功能,演示如下 -

class ArtisanCommandTest extends TestCase{
   public function testBasicTest() {
      $this->artisan('nova:create', [
         'name' => 'My New Admin panel'
      ])
      ->expectsQuestion('Please enter your API key', 'apiKeySecret')
      ->expectsOutput('Authenticating...')
      ->expectsQuestion('Please select a version', 'v1.0')
      ->expectsOutput('Installing...')
      ->expectsQuestion('Do you want to compile the assets?', 'yes')
      ->expectsOutput('Compiling assets...')
      ->assertExitCode(0);
   }
}

程式碼說明

在此,在測試案例模組下建立了名為“ArtisanCommandTest”的新類。它包含一個基本的函式 testBasicTest,其中包含各種斷言的功能。

artisan 命令 expectsQuestion 包含兩個屬性。一個帶有問題,另一個帶有 apiKeySecret。此處,artisan 驗證了 apiKeySecret 並驗證了使用者傳送的輸入。

同樣的情況也適用於問題“請選一個版本”,使用者需要在這裡提到一個具體版本。

廣告