You need to log in as a user first, try this in your test method:
$user = factory(User::class)->create();
$this->be($user);
Doesn't your post have a user_id attirubute there? If so, pass in the user_id too.
No sure if I should post it here or create a discussion so apologies if this isn't the right place to do it. Anyway,
I am going through build-a-laravel-app-with-tdd and I have followed along as one does however when I run phpunit, I get the error (pasted below my code). I am not sure why this is happening and would appreciate some help. My phpunit.xml has been updated so that I am using sqlite and memory for testing purposes and not my actual local database.
If I switch the test to "assertDatabaseMissing" it passes absolutely fine, so it seems like it is not inserting rows that it can read from?? idk!
ProjectsTest.php
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProjectsTest extends TestCase
{
use WithFaker, RefreshDatabase;
/** @test */
public function a_user_can_create_a_project()
{
$this->withoutExceptionHandling();
$attributes = [
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph
];
// $attributes = [
// 'title' => 'This title is hardcoded...',
// 'description' => 'Figure out how to make faker work in 6.0'
// ];
$this->post('/projects', $attributes);
$this->assertDatabaseHas('projects', $attributes);
// $this->get('/projects')->assertSee($attributes['title']);
}
}
CLI Output
There was 1 failure:
1) Tests\Feature\ProjectsTest::a_user_can_create_a_project
Failed asserting that a row in the table [projects] matches the attributes {
"title": "Qui quo dolorem in numquam excepturi mollitia.",
"description": "Omnis aut fuga sit atque culpa exercitationem. Vitae commodi aliquid voluptatem ipsam. Fuga quam et in assumenda dolorem corporis qui."
}.
The table is empty.
/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:24
/Users/damienk.sedgwick/Sites/birdboard/tests/Feature/ProjectsTest.php:32
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Thanks in advance for your help :-)
Please or to participate in this conversation.