$job doesn't have an id because you make it rather than persisting it. You would need either (i) to return the job from the Controller method; or (ii) assume (because of DatabaseMigrations trait) that the id must be 1 or (iii) do a App\Job::where(...)->first(); using properties of the factory Job instance or (iv) interrogate the the redirect to get the id
Apr 30, 2017
4
Level 8
id returning empty from within test
I'm following this episode https://laracasts.com/series/lets-build-a-forum-with-laravel/episodes/6 of Let's Build A Forum with Laravel and TDD series for a project of mine. The test is failing due to the fact that $job->id is returning empty, hence the path ends up being /jobs/ instead of something like /jobs/1. Below is the code I have:
Test
public function an_authenticated_user_can_create_a_job()
{
$this->actingAs(factory(User::class)->create());
$job = factory(Job::class)->make();
$this->post('/jobs', $job->toArray());
$this->get('/jobs/' . $job->id)
->assertSee($job->title)
->assertSee($job->description);
}
Controller
public function storeRequest $request)
{
$job = Job::create([
'user_id' => auth()->id(),
'title' => $request->title,
'description' => $request->description,
]);
return redirect('/jobs/' . $job->id);
}
Can't point out what went wrong.
Level 104
If you are using DatabaseMigrations, I thinks it's okay because there is only one job being created in that test / implementation, but you are probably better to test that the job is in the database and leave the GET request to a different test:
$this->assertDatabaseHas('jobs', [
'title' => $job->title
]);
Please or to participate in this conversation.