0

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.

1 Answer 1

1

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
        ]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.