2
user.php的 - 相当香草的hasMany关系:的PHPUnit + Laravel 5.2 +属性不返回正确
public function transactions()
{
return $this->hasMany('App\Transaction');
}
UserTest.php:
public function testTransactionsAttribute()
{
$user = Auth::user();
// Verify logged in user is expected user.
$this->assertTrue($user->username == 'something');
// Pay something.
$transaction = $user->payMembershipFee();
// Verify transaction is in database
$this->seeInDatabase('transactions', ['amount' => 50, 'user_id' => $user->id]);
// Verify transaction belongsTo relationship
$this->assertTrue($transaction->user->username == 'something');
// Verify user hasMany relationship - fails
$this->assertTrue($user->transactions->count() > 0, 'The user should have at least one transaction associated.');
}
这里就是它有趣的(我并没有修改数据库表 - 刚刚离开的PHPUnit并切换到叮叮):
$ php artisan tinker
抢用户(验证为创建相同的用户从测试):
$user = App\User::first()
复制/粘贴断言:
$user->transactions->count()
=> 1
此外,当我手动经过的步骤当地 - 它的工作原理。所以,看起来Laravel 5.2是按预期行事的。但是,PHPUnit不是。
我想知道是否有可能我错过Laravel 5.2和PHPUnit一起工作的方式?
如果在最后一个断言之前放置** $ user = $ user-> fresh(); **,会发生什么?我怀疑该事务可能不会同步,除非获取新实例。 – macghriogair
谢谢@macghriogair让我走下正确的道路。多谢。如果你想获得信用,我会非常乐意把它给你 - 只要发表一个类似于我写的答案 - 扩大你的评论。再次感谢 - 救生员。 –
没关系,快乐我可以帮助:) – macghriogair