Laravel基本测试断言为false。。但是它';什么有用?

Laravel基本测试断言为false。。但是它';什么有用?,laravel,testing,eloquent,Laravel,Testing,Eloquent,我的测试套件失败了,但我的代码在我的播种机中正常运行 当我在播种时,一旦我失败了。。但是当我运行两次时,我不再在以前的数据集上出现故障(只在新的数据集上)。我的功能是否太慢而无法执行 我的功能测试: /** * Test if User can follow an other user and send a notification * * @return void */ public function test_user_can_follow_a

我的测试套件失败了,但我的代码在我的播种机中正常运行

当我在播种时,一旦我失败了。。但是当我运行两次时,我不再在以前的数据集上出现故障(只在新的数据集上)。我的功能是否太慢而无法执行

我的功能测试:

/**
     * Test if User can follow an other user and send a notification
     *
     * @return void
     */
    public function test_user_can_follow_an_other_user()
    {
        $follower = factory(User::class)->create();
        $followed = factory(User::class)->create();

        $this->actingAs($follower, 'web')
             ->post('/following', ['user' => $followed->id])
             ->assertRedirect('/following');

        $this->assertTrue($follower->follows($followed));
    }
我的PhpUnit结果:

PHPUnit 7.1.5 by Sebastian Bergmann and contributors.

.mpoo.F                                                                 3 / 3 (100%)

Time: 2.91 seconds, Memory: 50.00MB

There was 1 failure:

1) Tests\Feature\UserTest::test_user_can_follow_an_other_user
Failed asserting that false is true.

/Users/gtr/Code/La-dechetterie-du-web/lddw-api/tests/Feature/UserTest.php:69

FAILURES!
Tests: 3, Assertions: 9, Failures: 1.
我的播种机:

public function run()
{
    $follower = User::first();
    $following = User::all();
    $followingArrayIds = [];

    foreach ($following as $u) {
        $follower->follow($u);

        array_push($followingArrayIds, [
            'follower' => $follower->id,
            'pretend_to_follow' => $u->id,
            'is '. $follower->id .' following '. $u->id .' ?' => $follower->follows($u) // should be true !!!!! 
        ]);
    }

    print_r($followingArrayIds);
}
我的模型的功能:

/**
     * User's following relation
     *
     * @return Relation User
     */
    public function following(){
        return $this->belongsToMany('App\User', 'followers', 'follower_id', 'following_id');
    }

    /**
     * User's followers relation
     *
     * @return Relation User
     */
    public function followers(){
        return $this->belongsToMany('App\User', 'followers', 'following_id', 'follower_id');
    }

    /**
     * Follow an other user
     *
     * @param User $user
     * @return void
     */
    public function follow(User $user){
        if($this->follows($user))
            return;

        $this->following()->attach($user);
    }

    /**
     * Check if the current user follows the user he wanted to
     *
     * @param User $user
     * @return boolean
     */
    public function follows(User $user){
        return $this->following->contains($user);
    }

Thx:)

使用
fresh()
获取
$follower
的新副本应该可以做到以下几点:

$this->assertTrue($follower->fresh()->follows($followed));

使用
fresh()
获取
$follower
的新副本应该可以做到以下几点:

$this->assertTrue($follower->fresh()->follows($followed));