【发布时间】:2015-09-07 04:50:53
【问题描述】:
我只是在使用 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 并检查迁移表,测试数据库为空,似乎没有发生迁移。
为了完成这项工作,我在文档中遗漏了什么?重用用户工厂就可以通过,但是不明白为什么不能访问测试数据库,是不是需要初始迁移?
【问题讨论】:
标签: php laravel phpunit laravel-5.1