PHP版本:7.39
Laravel版本:8
單元測試程式碼:
<?php
namespace Tests\Unit;
use App\Models\Item;
use PHPUnit\Framework\TestCase;
use App\Models\ItemScene;
class ItemTest extends TestCase
{
/** @test */
public function a_item_has_many_item_scenes()
{
$item = Item::factory()->create();
ItemScene::factory()->create(['item_id' => $item->id]);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Relations\HasMany', $item->itemScenes());
}
}
工廠程式碼:
<?php
namespace Database\Factories;
use App\Models\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
class ItemFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Item::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'area_name' => '深圳市',
'dept_name' => $this->faker->name,
'item_name' => $this->faker->name,
'item_code' => $this->faker->randomNumber(),
'manage_type' => $this->faker->randomElement(['A', 'B', 'C', 'D']),
'implement_code' => $this->faker->md5,
'remark' => $this->faker->sentence,
];
}
}
錯誤資訊
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
E 1 / 1 (100%)
Time: 00:00.139, Memory: 10.00 MB
There was 1 error:
1) Tests\Unit\ItemTest::a_item_has_many_item_scenes
InvalidArgumentException: Unknown formatter "name"
/home/vagrant/code/item_field_trace_back/vendor/fakerphp/faker/src/Faker/Generator.php:248
/home/vagrant/code/item_field_trace_back/vendor/fakerphp/faker/src/Faker/Generator.php:228
/home/vagrant/code/item_field_trace_back/vendor/fakerphp/faker/src/Faker/Generator.php:285
/home/vagrant/code/item_field_trace_back/database/factories/ItemFactory.php:27
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:382
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:361
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:345
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:157
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:350
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:318
/home/vagrant/code/item_field_trace_back/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:230
/home/vagrant/code/item_field_trace_back/tests/Unit/ItemTest.php:14
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
本作品採用《CC 協議》,轉載必須註明作者和本文連結