Laravel-单元测试-单元测试和单元测试失败时,整数数据类型的变量作为字符串出现 [英] Laravel - Unit test - variable of integer data type is coming as string while unit testing and the unit test is failing
问题描述
我是Laravel和PHP的新手.我创建了一个迁移``汽车''表,其中具有ID,制造商,型号,年份列.我已经使用播种机使用造假者制造了50辆汽车. 我已经编写了一个单元测试来测试year属性的数据类型为int,但是我的单元测试失败了.有人可以帮我吗?
I'm new to Laravel and PHP. I have created a migration 'Car' table with columns id, Make, Model, Year.I have used the seeder to make 50 cars using faker. I have written a unit test to test the data type of the year property to be int, but my unit test is failing. Could anyone pls help me on this?
Migration table:
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('make');
$table->string('model');
$table-> integer('year');
$table->timestamps();
});
Factory:
$factory->define(App\Car::class, function (Faker $faker) {
return [
'make' => $faker->randomElement($array = array ('Ford','Honda','Toyota')),
'model' => $faker->name,
'year' => $faker->year($max = 'now'),
Seeder
public function run()
{
factory(App\Car::class, 50)->create()->each(function ($car)
}
Unit test
public function testCarYearDataType()
{
$car = Car::find(1);
dd($car->year);
dd(gettype($car->Year));
this->assertInternalType('int',$car->Year);
}
推荐答案
不确定数据类型是否是正确的测试,但是无论如何
Not sure datatype is the right kind of test but anyway
要记住的关键是您需要一个不同的数据库来测试我使用内存的情况,因此我的phpunit.xml如下所示
The key thing to remember is you need to have a different database for testing my case I use memory so my phpunit.xml looks like the following
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
您将拥有自己的测试班.在您的情况下,我将其称为CarTest.php
The you will have your test class. In your case I would call it CarTest.php
<?php
namespace Tests\Unit;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;
class CarTest extends TestCase {
use DatabaseTransactions;
public function testCarYearDataType()
{
//just create 1 car as all the cars will have same data type anyway
factory(App\Car::class)->create();
this->assertInternalType('int', gettype(Car::first()->year));
}
}
这篇关于Laravel-单元测试-单元测试和单元测试失败时,整数数据类型的变量作为字符串出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!