0

我是 Laravel 的新手。我有 2 表产品和产品。另外,我有 2 家工厂 ProductionFactory 和 ProductFactory。我想通过 phpunit 测试它们。他们的连接是通过 production_id。

错误是 ErrorException:未定义的变量:生产。

我不明白。

提前致谢。

这是一个代码。

生产工厂.php

        $factory->define(App\Production::class, function (Faker $faker) {
        return [
                'name' =>$faker->name,
            ];
          });

产品工厂.php

       $factory->define(App\Product::class, function (Faker $faker) {
        $production_id = App\Production::pluck('id');

       if(!$production_id->isEmpty()){
          $production = $production_id->random();
       }

    return [
             'id' =>$faker->uuid,
             'name' =>$faker->name,
             'price' => $faker->numberBetween($min = 100, $max = 900),
             'description' =>Str::random(10),
             'production_id'=> $production,
          ];

生产测试.php

           class ProductionTest extends TestCase
            {
              use RefreshDatabase;

             /**
             * A basic unit test example.
             * @test
             * @return void
               */
            public function production()
               {
                   factory(Production::class)->make();
                   $this->assertTrue(true);
               }
           }

产品测试.php

           class ProductTest extends TestCase
            {
              use RefreshDatabase;

             /**
             * A basic unit test example.
             * @test
             * @return void
               */
            public function product()
               {
                   factory(Product::class)->make();
                   $this->assertTrue(true);
               }
           }
4

2 回答 2

0

模型制作

      use Illuminate\Database\Eloquent\SoftDeletes;

       class Production extends Model
       {
            use SoftDeletes;
              protected $fillable = [
                 'name',
              ];
             public function products()
               {
              return $this->hasMany('App\Product');

               }

模型产品

      class Product extends Model
          {

              use SoftDeletes;
               protected $fillable = [
              'id','name','price','description','production_id'
               ];
           public function production()
          {
             return $this->belongsTo('App\Production');
            }

SQLSTATE[HY000]:一般错误:1364 字段“production_id”没有默认值

于 2019-06-01T09:23:50.103 回答
0

这取决于应该测试什么功能。

检查模型关系的基本测试是否设置正确?

我将一个工作演示推送到GitHub (https://github.com/.../laravel-basic-relationship-tests)

应用程序/产品.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function production()
    {
        return $this->belongsTo(Production::class);
    }
}

应用程序/生产.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Production extends Model
{
    public function products()
    {
        return $this->belongsTo(Product::class);
    }
}

产品厂

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Product as Model;
use Faker\Generator as Faker;

$factory->define(Model::class, function (Faker $faker) {
    $production = factory(\App\Production::class)->make();
    $production->save();

    return [
        'name' => $faker->name,
        'price' => $faker->numberBetween($min = 100, $max = 900),
        'description' => $faker->text,
        'production_id' => $production->id
    ];
});

生产工厂

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Production as Model;
use Faker\Generator as Faker;

$factory->define(Model::class, function (Faker $faker) {
    return [
        'name' => $faker->name
    ];
});

$factory->afterCreatingState(Model::class, 'seed', function ($production, $faker) {
    $product = factory(\App\Product::class)->make();
    $production->products()->associate($product);
});

产品测试

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ProductTest extends TestCase
{
    /**
     * @return void
     */
    public function testProductHasWorkingProductionRelationship()
    {
        $product = factory(\App\Product::class)->create();
        $this->assertNotEmpty($product->production);
    }
}

生产测试

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ProductionTest extends TestCase
{
    /**
     * @return void
     */
    public function testProductionHasWorkingProductRelationship()
    {
        $production = factory(\App\Production::class)->state('seed')->create();
        $this->assertNotEmpty($production->products);
    }
}

希望这是任何进一步测试的一个很好的起点。

于 2019-06-01T01:09:00.727 回答