lundi 5 février 2018

Laravel 5.5 feature test says it's missing a field when the factory provides it

My product factory contains a slug:

$factory->define(Product::class, function (Faker $faker) {
    return [
        'title'       => $faker->sentence(4, true),
        'slug'        => $faker->slug,
        'description' => $faker->paragraph,
    ];
});

My feature test uses the factory to make a new instance:

$product = factory(Product::class)->make();
$response = $this->post('admin/products/store', $product->toArray());
$editPage = $this->get($response->headers->get('Location'));
$editPage->assertSeeText($product->title);

When I run the test I get a SQL error saying the slug is missing:

Error creating product:SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: products.slug (SQL: insert into "products" ("title", "description", "updated_at", "created_at") values (Velit quisquam et accusamus debitis et., Aperiam sint architecto ut sit pariatur aliquid. Et et omnis perspiciatis. Est officiis accusamus enim ut rerum cumque., 2018-02-05 23:58:02, 2018-02-05 23:58:02))

The same approach works in my other feature tests. The test is run as an authenticated admin will all the necessary permissions.

Aucun commentaire:

Enregistrer un commentaire