Home > Software design >  Laravel Feature Test - Inserting Data
Laravel Feature Test - Inserting Data

Time:06-16

I have a metadata table and need to test inserting data:

public function test_insert_unique_data()
{
    $response = Metadata::create([
        'metable_id' => '1',
        'metable_type' => File::class,
        'type' => 'resolution',
        'key' => 'width',
        'value' => 1000,
    ]);

    $response->assertCreated();
}

This is the migration for the table:

Schema::create(..., function (Blueprint $table) {
    $table->id();
    $table->bigInteger('metable_id')->unsigned()->index();
    $table->string('metable_type')->index();
    $table->string('type')->default('default');
    $table->string('key')->index();
    $table->text('value');
    $table->timestamps();
});

It's failing every time so I can only assume the test is written wrong. Any pointers please?

CodePudding user response:

Why not just use a factory? They have all sorts of assertions in their Database testing section.

Unless there's a reason for it, every testing that uses any models I'm seeing in modern Laravel (I come from Laravel 3-5 days) is using factories.

CodePudding user response:

I don't know about this assertCreated function but I know of something similar:

public function test_insert_unique_data()
{
    $metadata = Metadata::create([
        'metable_id' => '1',
        'metable_type' => File::class,
        'type' => 'resolution',
        'key' => 'width',
        'value' => 1000,
    ]);

    $this->assertModelExists($metadata);
}
  • Related