Now this started to fail after I added a bunch more tests because when it arrives at this it fails to match the ID of 1 is equal to 7.
How can I get the ID of the new book created in the post or have a clean state after each test?
It depends what your $attributes are. Are you calling factory(Post::class)->create() above, because that will persist a row, in which case the ID will get incremented.
What I usually do is use $post = factory(Post::class)->raw() and pass that to the post, then just use $post->fresh()->id which should give you the correct id.
@jove Yes, so the raw returns an array. So basically this is the flow that I use:
$post = factory(Post::class)->raw([ 'user_id' => $user->id ]);
$response = $this->call('post', route('dmin.books.store'), $post)
->assertSuccessful();
// here because I return the created resource from the controller:
$response->id; // is what I need