如何在Laravel 5中对我的控制器进行单元测试?

时间:2015-07-23 11:38:03

标签: unit-testing laravel phpunit laravel-5

我有下面的控制器,如何对destroy函数进行单元测试?

/**
     * Delete Bank by Id
     *
     * @param int $id
     */
    public function destroy($id)
    {
        $this->bankService->delete($this->bankService->findById($id, new ApiRequest()));
    }

1 个答案:

答案 0 :(得分:1)

您提供的信息非常少。这是一个通用的测试:

class BanksControllerTest extends TestCase
{
    use Illuminate\Foundation\Testing\DatabaseTransactions;

    public function testDeleteById()
    {
        $user = App\User::findOrFail(1); // The user required to access the controller, if any
        $id = 1; // The id of the bank you want to delete
        $page = route('banks.destroy', $id); // The URL of the controller
        $table = 'banks'; // The table that contains the banks

        $this->actingAs($user)->visit($page)->notSeeInDatabase($table, ['id' => $id]);
    }
}

显然你需要根据你的情况进行调整