Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing Codeception如何测试用户是否具有角色_Testing_Laravel_Relationships_Codeception - Fatal编程技术网

Testing Codeception如何测试用户是否具有角色

Testing Codeception如何测试用户是否具有角色,testing,laravel,relationships,codeception,Testing,Laravel,Relationships,Codeception,我正在努力养成边写测试的习惯。 我正在使用Laravel4.2构建一个应用程序,该应用程序注册用户并为不同的用户类型分配不同的角色 我想写一个测试,检查用户是否有角色。我使用委托管理我的角色/权限,使用laracast/TestDummy生成虚拟对象。 角色存储在一个透视表中,因此我基本上是在测试多对多关系的存在性 TestDummy文档说明我需要在fixtures.yml文件中定义我的对象及其关系,并给出了一个示例 Post: title: Hello World $string bo

我正在努力养成边写测试的习惯。 我正在使用Laravel4.2构建一个应用程序,该应用程序注册用户并为不同的用户类型分配不同的角色

我想写一个测试,检查用户是否有角色。我使用委托管理我的角色/权限,使用laracast/TestDummy生成虚拟对象。 角色存储在一个透视表中,因此我基本上是在测试多对多关系的存在性 TestDummy文档说明我需要在fixtures.yml文件中定义我的对象及其关系,并给出了一个示例

Post:
  title: Hello World $string
  body: $text
  published_at: $date
  author_id:
    type: Author

Author:
    name: John Doe $integer
上述布局定义了一对一关系。帖子有作者。 我试图测试的是一个用户有一个角色,其中许多用户可以有许多角色

我想知道如何在fixtures.yml文件中定义它 在我心里

如果我想测试多对多关系(许多用户有许多角色),我可以这样引用它吗

Excel\Users\User:
  username: $string$integer
  email: $string$integer@example.com
  password: $string
  created_at: $date
  updated_at: $date

\Role:
  name: $string
  created_at: $date
  updated_at: $date

Assigned_Role:
  user_id:
    type: Excel\Users\User
  role_id:
    type: \Role
我看到的问题是,分配的_角色没有模型,因为它只是一个透视表


如何测试用户是否具有特定角色?

TestDummy仅支持生成属于关系。假设您想在
User
上测试方法,比如
hasRole($role)
,您可以尝试类似的方法


Assigned_Role是数据透视表的名称,没有附加模型。但是,当我执行$role=Factory::create('\role')时,这非常有效。谢谢你的反馈。哎哟-现在就搞定答案!
$user = Factory::create('Excel\Users\User');
$role = Factory::create('Role');

$user->roles()->attach($role);
$this->assertTrue($user->hasRole($role));

$user->roles()->detach($role);
$this->assertFalse($user->hasRole($role));