2 years ago

static::deleting in a boot method is not working in test

Posted 2 years ago by steveperrycreative

This is my test:

/** @test */
public function deleting_a_task_from_a_project_restores_its_credits()

    $project = factory('App\Project')->create();

    $task = factory('App\Task')->create([
        'project_id' => $project->id,
        'credits_used' => 20

    $project->credits = 100;

    $response = $this->json('DELETE', $task->path());


    $this->assertTrue($project->credits == 120);

My destroy method on the TasksController is:

public function destroy(Task $task)
    $project = $task->project;

    $this->authorize('update', $project);


    if (request()->wantsJson()) {
        return response([], 204);

    return redirect($project->path());

And my boot method on the Task model is:

protected static function boot()

    static::creating(function ($task) {

    static::deleting(function ($task) {

The add and remove credits methods just perform some basic maths and save, nothing fancy. When I test creating a task the credits get removed from the project as expected. However when I run the above delete test nothing happens to the credits on the project, they stay at 100.

The boot method runs fine. I've even tried sliming it by just doing:

static::deleting(function ($task) {
    $task->project->credits = 120;

But that doesn't work either. I'm stumped. Am I missing something silly!?

Please sign in or create an account to participate in this conversation.