3

I delete post:

$this->delete('/api/posts/1');

How check if that delete?

$this->assertNull(Post::find($post->id)); 

It doesn't work!

Georg
  • 107
  • 1
  • 10

4 Answers4

3

try this :

$this->notSeeInDatabase('posts', ['id' => $post->id]);

description :

->notSeeInDatabase($table, array $data)  

and

->missingFromDatabase($table, array $data)

One is just an alias for the other.

yoeunes
  • 2,927
  • 2
  • 15
  • 26
1

Most answers are way to complex or use a extra function to call. The Laraval delete function returns a boolean or a null

/**
 * Delete the model from the database.
 *
 * @return bool|null
 * @throws \Exception
 */
public function delete()
{
  ....
  ....
}

So you can simply can use this code to check a delete.

try {
  if ($this->delete('/api/posts/1')) {
  }    
} catch (\Exception $exception) {}
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
0

You could check it with Post::findOrFail(), or you could just check this if(Post::findById($id) == null).

Edit: not sure if this still works in Laravel >5.2 but you might be able to fetch the collection withTrashed() if you soft-delete the record. Then you could use if($record->isTrashed()) { ... }. This functionality isn't default in a Model, but is present in the SoftDelete trait.

Found the information for the edit here: link.

jbehrens94
  • 2,356
  • 6
  • 31
  • 59
0

YOu should try this:

$post = Post::find($post->id); 

if(is_null($post))

Hope this work for you !!!

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57