Summer Sale! All accounts are 50% off this week.

Shivamyadav's avatar

project_id doesn't have a default value while testing , don't understand?

showing me this error,where i am doing wrong , help me out sir? controller

 public function store(Project $project)
    {
        request()->validate(['body' => 'required']);
        $project->addTask(request('body'));
        return redirect($project->path());
    }

test code

 public function test_only_the_owner_of_a_project_can_add_a_task()
    {
        $this->withoutExceptionHandling();

        $this->signIn();    
        $project = Project::factory()->create();
        $this->post($project->path() .'/tasks', ['body' => 'Test task'])
            ->assertStatus(403);
        $this->assertDatabaseMissing('tasks', ['body'=> 'Test Task' ]);        
    }

error

1) Tests\Feature\ProjectTasksTest::test_only_the_owner_of_a_project_can_add_a_task
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'project_id' cannot be null (SQL: insert into `tasks` (`body`, `project_id`, `updated_at`, `created_at`) values (Test task, ?, 2022-12-05 18:32:54, 2022-12-05 18:32:54))

0 likes
24 replies
Tray2's avatar

Simple, a task is connected to a project right?

Then you need to pass the project id when creating the task. So my guess is that your Project is null

Sinnbeck's avatar

It looks like there is an error in your code when trying to insert a new task into the database. The error message mentions that the project_id column cannot be null. This means that when trying to insert a new task, the value for the project_id column is missing.

One potential issue in your code is that you are not passing the project_id when inserting the new task. In the store method, you are calling the addTask method on the $project object, but it's not clear if this method is inserting the project_id value along with the new task.

If the addTask method does not insert the project_id value, then you will need to modify this method to include the project_id when inserting the new task. Alternatively, you can modify the store method to explicitly insert the project_id value along with the new task.

Another potential issue is that the $project object in the store method may not have a valid project_id value. This could happen if the $project object is not being correctly retrieved from the database, or if the $project object is not being passed to the store method correctly.

To fix this issue, you will need to check that the $project object is being correctly retrieved and passed to the store method, and that the addTask method is inserting the project_id value along with the new task.

Shivamyadav's avatar

@Sinnbeck here is my addTask method

public function addTask($body)
    {
        return $this->tasks()->create(compact('body'));   
    }
Shivamyadav's avatar

@Sinnbeck

Route::group(['middleware' => 'auth'], function () {
    Route::get('/projects', [ProjectsController::class, 'index']);
    Route::get('/projects/create', [ProjectsController::class, 'create']);
    Route::get('/projects/{project}', [ProjectsController::class, 'show']);
    Route::post('/projects', [ProjectsController::class, 'store']);
    Route::post('/projects/{projects}/tasks', [ProjectTasksController::class, 'store']);

});
Shivamyadav's avatar

project path

  public function path()
    {
        // return "/projects/" .$this->id;
        return "/projects/{$this->id}";
    }

Sinnbeck's avatar

@Shivamyadav here it's projects with an s

Route::post('/projects/{projects}/tasks', [ProjectTasksController::class, 'store']);

And here it's project

public function store(Project $project)
    { 
1 like
Shivamyadav's avatar

showing this error

1) Tests\Feature\ProjectTasksTest::test_only_the_owner_of_a_project_can_add_a_task
Failed asserting that a row in the table [tasks] does not match the attributes {
    "body": "Test Task"
}.

Found similar results: [
    {
        "body": "Test task"
    }
].
Sinnbeck's avatar

@Shivamyadav you are passing a body, and that would make it insert the task.? But your test assumes it doesn't

Shivamyadav's avatar

@Sinnbeck showing me this error

1) Tests\Feature\ProjectTasksTest::test_a_project_can_have_tasks
Symfony\Component\HttpKernel\Exception\HttpException:

for this test

  public function test_a_project_can_have_tasks()
    {
        $this->withoutExceptionHandling();
        $this->signIn();   
        $project = Project::factory()->create(['owner_id' => auth()->user()->id]);
        $this->post($project->path() .'/tasks', ['body' => 'Test task']);
        $this->get($project->path())->assertSee('Test Task');
    }
Shivamyadav's avatar

@Sinnbeck like this sir

  public function test_a_project_can_have_tasks()
    {
        $this->withoutExceptionHandling();
        $this->signIn();   
        $project = Project::factory()->create(['owner_id' => auth()->user()->id]);
    
        $this->get($project->path(). '/tasks')->assertSee('Test Task');
    }

Sinnbeck's avatar

@Shivamyadav that's a completely different test?

 public function test_only_the_owner_of_a_project_can_add_a_task()
    {
        $this->signIn();    
        $project = Project::factory()->create();
        $this->post($project->path() .'/tasks', ['body' => 'Test task'])
            ->assertStatus(403);
        $this->assertDatabaseHas('tasks', ['body'=> 'Test Task' ]);        
    }
Shivamyadav's avatar

@Sinnbeck but i done like that it tells me this

1) Tests\Feature\ProjectTasksTest::test_a_project_can_have_tasks
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: The GET method is not supported for this route. Supported methods: POST.

Please or to participate in this conversation.