jeudi 9 janvier 2020

Factory counts 2 models instead of 1 in tests

I have these migrations:

class CreateOrdersTable extends Migration
{
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });
    }
}
class CreatePaymentsTable extends Migration
{
    public function up()
    {
        Schema::create('payments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('order_id');
            $table->timestamps();

            $table->foreign('order_id')->references('id')->on('orders');
        });
    }
}

and these factories:

$factory->define(Payment::class, function (Faker $faker) {
    return [
        'order_id' => factory(Order::class)->create(),
    ];
});
$factory->define(Order::class, function (Faker $faker) {
    return [
        'user_id' => factory(User::class)->create(),
    ];
});

Now in my test i have this:

/** @test */
public function it_should_count_1_order()
{
     $order = factory(Order::class)->create();

     $payment = factory(Payment::class)->create([
         'order_id' => $order->id,
     ]);

     $this->assertEquals(1, Order::count())
}

The Order table count gives me 2. why? it should be 1 since I am telling payment factory to override order_id with the given order. Am I missing something?

Aucun commentaire:

Enregistrer un commentaire