【问题标题】:How can I unit test my controller in Laravel 5?如何在 Laravel 5 中对控制器进行单元测试?
【发布时间】:2015-10-13 16:37:53
【问题描述】:
我有以下控制器,如何对销毁功能进行单元测试?
/**
* Delete Bank by Id
*
* @param int $id
*/
public function destroy($id)
{
$this->bankService->delete($this->bankService->findById($id, new ApiRequest()));
}
【问题讨论】:
标签:
unit-testing
laravel
phpunit
laravel-5
【解决方案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]);
}
}
显然你需要根据你的场景来调整它