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

时间:2018-06-06 09:02:15

标签: php laravel unit-testing phpunit 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无法找到明确的指示下一步做什么?

1 个答案:

答案 0 :(得分:0)

<?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
        ]);
    }
}
?>