EbrahemSamer's avatar

Some Tests do not work after adding policies to model?

I've some database tests

` public function testUpdatePost() { $post = $this->createDummyPost(); $this->assertDatabaseHas('posts', [ "title" => "This is title for test", "content" => "This is content for test", ]);

    $params = [
        'title' => 'x Updated Title here',
        'content' => 'x Updated Content here',
        'user_id' => 1,
    ];

    $this->put("/posts/{$post->id}", $params)->assertStatus(302)->assertSessionHas('status');
    $this->assertEquals(session('status'), 'Post has been updated');

    // Original Post has been gone.
    $this->assertDatabaseMissing('posts', $post->toArray());
}

public function testDeletePost()
{   

    $post = $this->createDummyPost();
    $this->assertDatabaseHas('posts', [ 
        "title" => "This is title for test",
        "content" => "This is content for test",
    ]);

    $this->delete("/posts/{$post->id}")->assertStatus(302)->assertSessionHas('status');
    $this->assertEquals(session('status'), 'Post has been deleted');

    $this->assertSoftDeleted('posts', [ 
        "title" => "This is title for test",
        "content" => "This is content for test",
    ]);
}

private function createDummyPost($user_id = null) :Post
{
    return Post::factory()->newTitle()->create();
}

`

I've added policy on Post Model so no one can delete or update post unless he created the post he want to delete....

After making this policy and worked fine, these two test methods have failed, how can I run the test in this situation?

0 likes
6 replies
bugsysha's avatar

how can I run the test in this situation?

php artisan test --filter testUpdatePost and php artisan test --filter testDeletePost

1 like
bugsysha's avatar
bugsysha
Best Answer
Level 61

Not sure since I've never tried that. Probably you can, but how I do it is I name my test methods in a same way so I can easily run multiple.

From your test methods I would name them like this: testPostCanBeUpdated and testPostCanBeDeleted.

So then I do php artisan test --filter testPostCanBe and it will run both test methods.

1 like
bugsysha's avatar

Have you tried regex?

php artisan test --filter '/testUpdatePost|testDeletePost/'

bugsysha's avatar

Also try adding @group annotations and doing --group instead of --filter.

bugsysha's avatar

Also, I sort all my methods alphabetically and in combination with that specific naming style it is quite easy to navigate around.

Please or to participate in this conversation.