Laravel 8 每次測試后重置數據庫

2021-07-26 09:32 更新

在每次測試后重置數據庫通常很有用,這樣前一次測試中的數據就不會干擾后續(xù)測試。 RefreshDatabase trait 采用最佳方法來遷移測試數據庫,具體取決于您使用的是內存數據庫還是傳統(tǒng)數據庫。使用測試類中的 trait,將為您處理所有事情:

    <?php

    namespace Tests\Feature;

    use Illuminate\Foundation\Testing\RefreshDatabase;
    use Illuminate\Foundation\Testing\WithoutMiddleware;
    use Tests\TestCase;

    class ExampleTest extends TestCase
    {
        use RefreshDatabase;

        /**
         * A basic functional test example.
         *
         * @return void
         */
        public function testBasicExample()
        {
            $response = $this->get('/');

            // ...
        }
    } 
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號