我只是在使用 Laravel 5.1 学习 PHPUnit。我正在使用“使用 DatabaseMigrations”为每个测试迁移测试数据库,这是我在 phpunit.xml 中设置的:
<php>
...
<env name="DB_DATABASE" value="project_test"/>
...
</php>
在检查实例化、工厂等时,我已经设置了一堆基本测试,但我想检查 UserModel 中的访问器和修改器:
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
public function getPasswordAttribute($password)
{
$this->attributes[ 'password' ] = bcrypt($password);
}
但是当访问器测试运行时:
/**
* A basic check of full name accessor.
*
* @return void
*/
public function testCheckFullNameOfUser()
{
$user = User::all();
$this->assertEquals($user->first_name . ' ' . $user->last_name, $user->fullname);
}
我收到此错误:
1) UserTest::testCheckFullNameOfUser
ErrorException: Trying to get property of non-object
这似乎表明数据库尚未迁移,通过 SSH 进入 Homestead 并登录 MySQL 并检查迁移表,测试数据库为空,似乎没有发生迁移。
我在文档中遗漏了什么来完成这项工作?我可以通过重用用户工厂使其通过,但我不明白为什么我无法访问测试数据库,是否需要初始迁移?