ABHILASHA K.M
ABHILASHA K.M

Reputation: 164

How to test function in laravel Unit testing

Using Unit Testing for the first time.

Ceated Test File by cmd:

php artisan module:make-test ProjectTest Projects

I can test by cmd:

./vendor/bin/phpunit

But I want to test my function, My function is like:

public function update(Request $request, $id)
    {
        $project = Project::find($id);
        $project->project_name = request('project_name');
        $project->save();
        return response()->json([
            'message' =>  "Project Updated Successfully",
            'updatedId' => $project->id
            ], 200);
    }

Can anyone please guide me on how to test this Controller in PHPUnit?

On PostMan testing the function like:

URL:(POST method) http://localhost:8000/api/updateProject/1

On Body

{
    "project_name":"school",
}

How to use PHP Unit Testing in Laravel for the above Controller ?. Kindly explain to me with code Snippets.

Upvotes: 0

Views: 2037

Answers (1)

Killallskywalker
Killallskywalker

Reputation: 36

You can test by using HTTP test . In order to test this you can make a request to defined route .

public function testUpdateProject()
{
    $response = $this->json('POST', '/api/updateProject/project-id',$updatedData);

    $response
        ->assertStatus(201)
        ->assertJson([
           'field_that_return' = true
        ]);
}

Upvotes: 1

Related Questions