how can I run the test in this situation?
php artisan test --filter testUpdatePost and php artisan test --filter testDeletePost
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?
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.
Please or to participate in this conversation.