如何在测试期间禁用附加模型访问器?

问题描述 投票:0回答:1

正在编写测试,我需要使用Faker工厂制造商创建Eloquent模型Video的新实例

$user = create(User::class);
$video = create(Video::class, 'make')->toArray(); // toArray serializes the model to include accessors
$user->videos()->create($video); // <--- Error occurs here

PDOException:SQLSTATE [HY000]:常规错误:1个表视频没有名为视图的列

[create()是自动加载文件中factory()的辅助函数包装器

/**
 * Generate a fake model
 *
 * Call the factory helper function on given model
 *
 * @param Illuminate\Database\Eloquent\Model $model Eloquent Model
 * @param string $method create or make the model
 * @param int $times How many model instances to return
 * @param array $properties Model attributes to override in factory
 *
 * @return mixed Illuminate\Database\Eloquent\Model|array|collection
 **/
function create($model, $method = 'create', $times = null, $properties = [])
{
    return factory($model, $times)->$method($properties);
}

模型Video追加了一个views访问器(与数据库无关),这是模型

class Video extends BaseModel
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['views', 'length', 'timesReported'];
}

views访问器在BaseModel中,就是这个

class BaseModel extends Model
{
    public $guarded = []; // Yolo!!

    /**
     * Get the user who owns the model.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    // Get model views count from Redis
    public function getViewsAttribute()
    {
        return \Redis::zscore('popular_'.$this->getTable(), $this->id);
    }
}

这里是VideoFactory,在有帮助的情况下

$factory->define(App\Video::class, function (Faker $faker) {
    return [
        'title' => $faker->realText(50, 2),
        'uploader' => 'Unknown',
        'duration' => '00:00:00',
        'thumbnail' => $faker->imageUrl(),
        'poster' => $faker->imageUrl(),
        'slides' => $faker->imageUrl(),
        'hls' => $faker->url,
        'mp4' => $faker->url,
        '_3gp' => $faker->url,
        'quality' => $faker->randomElement(['normal', 'hd']),
    ];
});

videos表迁移

Schema::create('videos', function (Blueprint $table) {
    $table->increments('id');
    $table->json('title'); // MySQL doesn't allow uniqueness on json type columns
    $table->enum('quality', ['normal', 'hd']);
    $table->unsignedInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->string('uploader');
    $table->time('duration');
    $table->string('thumbnail')->unique();
    $table->string('poster')->unique();
    $table->string('slides')->unique();
    $table->string('hls')->unique();
    $table->string('mp4', 350)->unique();
    $table->string('_3gp', 350)->unique();
    $table->json('slug');
    $table->timestamps();
});

如何排除附加的访问器来模拟工厂调用?

我曾考虑使用array_except,但是每次附加另一个访问器时,我都必须修改测试]]

正在编写测试,我需要使用Faker工厂制造商创建Eloquent模型视频的新实例$ user = create(User :: class); $ video = create(Video :: class,'make')-> toArray(); // ...

laravel phpunit faker
1个回答
1
投票

您可以在关系上使用save而不是create,因为您已经有一个Video实例,该实例具有您要从工厂获得的属性:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.