【发布时间】:2018-09-05 08:00:05
【问题描述】:
我是 Laravel 和 PHP 的新手。我创建了一个迁移“汽车”表,其中包含 id、Make、Model、Year 列。我使用播种机使用 faker 制造了 50 辆汽车。 我已经编写了一个单元测试来测试 year 属性的数据类型是否为 int,但是我的单元测试失败了。谁能帮我解决这个问题?
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);
}
【问题讨论】:
-
首先,您不需要为您的数据库播种以进行测试。从技术上讲,测试是在不同的数据库中完成的。如果您想使用生产数据库,请确保您有连接到生产数据库的 .testing.env。
-
您是否在模型中使用类型转换?
标签: php laravel unit-testing