我想尝试进行测试,登录的成员可以创建一个作业,这是我的测试代码。
/** @test */
public function member_can_create_a_job(){
$member = factory('App\Models\M_member')->create();
$this->actingAs($member);
$job = factory('App\Models\M_lowker')->make();
$this->post('/lowker/tambah-lowker', $job->toArray())->assertRedirect('/lowker/tambah-lowker');
}
这是我的App \ Models \ M_member
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class M_member extends Model{
protected $table = "member";
public $timestamps = false;
protected $fillable = ["nama", "email", "password", "alamat", "tgl_lahir", "remember_token"];
public function jobs()
{
return $this->hasMany('App\Models\M_lowker');
}
public function comments()
{
return $this->hasMany('App\Models\M_komentar');
}
}
当我跑步时,我在cmd中得到错误 this
1)Tests \ Feature \ JPSTest :: member_can_create_a_job TypeError:传递给Illuminate \ Foundation \ Testing \ TestCase :: actsAs()的参数1必须是Illuminate \ Contracts \ Auth \ Authenticatable的实例,App \ Models \的实例M_member给出,在我的名字中调用:\ W 42 N \ Home Work \ Semester 5 \ Rekayasa Perangkat Lunak \ Praktikum \ jps \ tests \ Feature \ JPSTest.php 35行
我:\ W 42 N \ Home Work \第5学期\ Rekayasa Perangkat Lunak \ Praktikum \ jps \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Testing \ Concerns \ InteractsWithAuthentication.php:16 I:\ W 42 N \ Home Work \第5学期\ Rekayasa Perangkat Lunak \ Praktikum \ jps \ tests \ Feature \ JPSTest.php:35
错误!测试:3,断言:3,错误:1。
答案 0 :(得分:1)
此错误告诉您正在使用的模型未扩展Illuminate \ Contracts \ Auth \ Authenticatable合同,这是使用actsAs方法所必需的。如果你有laravel的auth,你可以检查用户模型作为一个例子。这是类似的东西:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
因此,请尝试扩展您的模型以获得此功能。
或者您可以像这样在您的模型上实现Authenticatable合同
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
答案 1 :(得分:0)
通过像这样修改我的M_member模型解决了这个问题。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class M_member extends Model implements Authenticatable{
use AuthenticableTrait;