Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

517
Views
Laravel: las fábricas creadas con valores booleanos en la columna no pasan el caso de prueba con assertDatabaseHas en la base de datos mysql

La pregunta

Tengo mi muestra de tabla con columnas has_value y has_condition que son de tipo booleano creadas por laravel en MySQL. En Factory of the sample table he configurado el valor faker para que sea booleano. En el caso de prueba, he pasado el valor del falsificador tal como está. Laravel almacena el valor correctamente, pero el caso de prueba con assertDatabaseHas falla ya que MySQL almacena los valores en valores numéricos, pero el falsificador proporciona el valor booleano.

La puesta en marcha

Migración

 Schema::create('sample', function (Blueprint $table) { $table->unsignedBigInteger(); $table->boolean('has_value')->default(false); $table->boolean('has_condition')->default(false); });

Modelo

 class Sample extends Model { /** * The attributes that aren't mass assignable. * * @var array */ protected $guarded = ['id', 'created_at', 'updated_at']; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'has_condition' => 'boolean', 'has_value' => 'boolean', ]; }

La fábrica

 $factory->define(Sample::class, function (Faker $faker) { return [ 'has_condition' => $faker->boolean, 'has_value' => $faker->boolean ]; });

el caso de prueba

 use DatabaseMigrations; public function test_is_being_stored() { $data = factory(Sample::class)->make()->toArray(); $user = factory(User::class)->create() $this->actingAs($user)->post(route('some.route.store'), $data) ->assertSessionHasNoErrors() ->assertStatus(200); $this->assertDatabaseHas('sample', $data) }

El resultado es el siguiente

 Failed asserting that a row in the table [campaign_tax_years] matches the attributes { "has_condition": true, "has_value": true }. Found: [ { "id": 27, "created_at": "2020-07-01 07:36:52", "updated_at": "2020-07-01 07:36:52", "has_condition": 1, "has_value": 1 } ]

Los cheques de valor en la base de datos no son cheques encasillados. Pero el valor que se almacena es correcto. No puedo entender por qué Laravel no emite los valores y luego verifica los tipos de columna.

Varias versiones de artículos

Laravel: 6.8.x Mysql: 5.7.29 Phpunit: ^8.0

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!