【发布时间】:2019-12-02 07:06:36
【问题描述】:
我正在尝试在我的用户模型上编写一个单元测试,以测试数据库中是否仍然存在软删除记录。
/**
* check if users are soft deleted only
*
* @return void
*/
public function testUserIsSoftDeleted()
{
$user = factory(User::class)->create();
$user->delete();
$this->assertSoftDeleted('users', $user->toArray());
}
在我向模型添加自定义属性之前,此测试运行良好。
<?php
namespace App;
use Laravel\Passport\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use OwenIt\Auditing\Contracts\Auditable;
class User extends Authenticatable implements MustVerifyEmail, Auditable
{
use HasApiTokens, Notifiable, SoftDeletes, HasRoles, \OwenIt\Auditing\Auditable;
protected $guard_name = 'web';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'password', 'active', 'activation_token', 'email_verified_at'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'activation_token'
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* The attributes that should be added to the JSON response
*
* @var array
*/
protected $appends = ['md5_email'];
/**
* Convert email address into md5 string
*
* @var string
*/
public function getMd5EmailAttribute()
{
return md5(strtolower(trim($this->email)));
}
}
当我运行测试时,出现以下错误。
如何在 Found 数组中包含自定义属性?
【问题讨论】:
-
它失败了,因为数据库中没有
md5_email列。您必须将其添加到数据库或在查询中跳过该参数 -
是的,这是正确的,因为它是模型中添加的属性。如何跳过参数让测试通过?
标签: php laravel unit-testing phpunit