Besoin d'aide ?

Unit Testing controller in laravel 8

Avatar de Souhi-stack
Souhi-stack

I am very new to testing in Laravel, I'm working in laravel 8, the function that I want to test is :

class OrderController extends Controller
{
    public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $newOrder = new Order;
            $newOrder->fill($request->all());
            $newOrder->user_id = $request->user()->id; 
            
            if (!$request->doAssigned) {
                $newOrder->status = ($request->doValidate) ? 'pend' : 'draft';
            }
            
            $newOrder->save();
            
            DB::commit();
            return $newOrder;
        } catch (\Exception $e) {
            DB::rollback();
            abort(400, $e);
        }
    }
}

I have tried to write this test :

public function testStore()
{
    Sanctum::actingAs($this->user, ['*']);
    $response = $this->postJson(action('\App\Http\Controllers\OrderController@store'), [
        'doAssigned' => false,
        'doValidate' => false,
    ]);

    $response->assertJsonPath('status', 'draft');
} 

But when I run my test I get this error:

1) Tests\Unit\appTest\HttpTest\ControllersTest\OrderControllerTest::testStore
Invalid JSON was returned from the route.

Thank you for any help!

Vous ne pouvez pas répondre à ce sujet.