Search code examples
phplaravelunit-testingphpunitlaravel-5.5

How to correctly write a unit test for controllers methods in Laravel 5.5?


Please check the code I used to in my controller:

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

On my request page, I wrote the codes below:

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

I try the artisan command like below

php artisan make:test Pages --unit`

But a clear instruction could not be found for laravel 5.5 what to do next?


Solution

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