编程语言
首页 > 编程语言> > php – 如何在Laravel 5.5中正确编写控制器方法的单元测试?

php – 如何在Laravel 5.5中正确编写控制器方法的单元测试?

作者:互联网

请检查我在控制器中使用的代码:

class ObjectsPagesController extends Controller
{
    public function destroy(Destroy $destroy, $id)
    {
        $objectsPage = ObjectsPages::with( 'ObjectsPagesRelation')->where('group_id', $id)->first();
        if (isset($objectsPage)) {

            $objectsPage->delete();
            $objectsPage->ObjectsPagesRelation()->delete();
            return redirect()->route('objects.pages.index')->with('success', 'done');  

        }else{
            abort(404);
        }
    }
}

在我的请求页面上,我写了以下代码:

class Destroy extends FormRequest
{
    public function authorize()
    {
        return Auth::user()->can('del_objects_pages');
    }
    public function rules()
    {
        return [
            //
        ];
    }
}

我尝试下面的工匠命令

php artisan make:test Pages --unit`

但是laravel 5.5无法找到明确的指示下一步做什么?

解决方法:

<?php 
  class StoreObjectPageTest extends TestCase
{
    public function testStoreObjectIsCreated()
    {
        $this->actingAs($someUser)->post('/some/url');

        $this->assertDatabaseHas('store_objects', [
            // The attributes of a row you're expecting to see in DB
        ]);
    }
}
?>

标签:laravel-5-5,php,unit-testing,laravel,phpunit
来源: https://codeday.me/bug/20190910/1799303.html