我删除了帖子:
$this->delete('/api/posts/1');
如何检查删除操作是否成功?
$this->assertNull(Post::find($post->id));
它不起作用!
试一下这个:
$this->notSeeInDatabase('posts', ['id' => $post->id]);
description :
->notSeeInDatabase($table, array $data)
并且
->missingFromDatabase($table, array $data)
大多数答案都太复杂或使用额外的函数进行调用。 Laravel的删除函数返回布尔值或null。
/**
* Delete the model from the database.
*
* @return bool|null
* @throws \Exception
*/
public function delete()
{
....
....
}
try {
if ($this->delete('/api/posts/1')) {
}
} catch (\Exception $exception) {}
你可以使用 Post::findOrFail()
进行检查,或者你也可以使用 if(Post::findById($id) == null)
进行检查。
编辑:不确定在 Laravel >5.2 中是否仍然有效,但如果你对记录进行了软删除,则可能能够获取集合 withTrashed()
。然后你可以使用 if($record->isTrashed()) { ... }
。这个功能在模型中不是默认的,但存在于 SoftDelete
trait 中。
在这里找到了编辑的信息:link。
你应该尝试这个:
$post = Post::find($post->id);
if(is_null($post))